<i id='VUrHe'><tr id='VUrHe'><dt id='VUrHe'><q id='VUrHe'><span id='VUrHe'><b id='VUrHe'><form id='VUrHe'><ins id='VUrHe'></ins><ul id='VUrHe'></ul><sub id='VUrHe'></sub></form><legend id='VUrHe'></legend><bdo id='VUrHe'><pre id='VUrHe'><center id='VUrHe'></center></pre></bdo></b><th id='VUrHe'></th></span></q></dt></tr></i><div id='VUrHe'><tfoot id='VUrHe'></tfoot><dl id='VUrHe'><fieldset id='VUrHe'></fieldset></dl></div>
    <legend id='VUrHe'><style id='VUrHe'><dir id='VUrHe'><q id='VUrHe'></q></dir></style></legend>

    <small id='VUrHe'></small><noframes id='VUrHe'>

      <bdo id='VUrHe'></bdo><ul id='VUrHe'></ul>

        <tfoot id='VUrHe'></tfoot>

        如何仅在 yii2 中选择后才呈现视图

        时间:2023-10-16
          <tbody id='TGLaz'></tbody>
      1. <legend id='TGLaz'><style id='TGLaz'><dir id='TGLaz'><q id='TGLaz'></q></dir></style></legend>
          • <bdo id='TGLaz'></bdo><ul id='TGLaz'></ul>

          • <tfoot id='TGLaz'></tfoot>

                <small id='TGLaz'></small><noframes id='TGLaz'>

              • <i id='TGLaz'><tr id='TGLaz'><dt id='TGLaz'><q id='TGLaz'><span id='TGLaz'><b id='TGLaz'><form id='TGLaz'><ins id='TGLaz'></ins><ul id='TGLaz'></ul><sub id='TGLaz'></sub></form><legend id='TGLaz'></legend><bdo id='TGLaz'><pre id='TGLaz'><center id='TGLaz'></center></pre></bdo></b><th id='TGLaz'></th></span></q></dt></tr></i><div id='TGLaz'><tfoot id='TGLaz'></tfoot><dl id='TGLaz'><fieldset id='TGLaz'></fieldset></dl></div>

                  本文介绍了如何仅在 yii2 中选择后才呈现视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在单个视图中渲染两个视图.

                  field($model, 't_type')->dropDownList(['' =>'请选择','基于平板' =>'基于板','基于 TOU' =>'基于 TOU']) ?><div class="showSlab";id="slab"样式=显示:无"><?php echo $this->render('_slabBased', ['modelsTariffSlabs' =>$modelsTariffSlabs,]);?>

                  <div class="showTou";id=头"样式=显示:无"><?php echo $this->render('_touBased', ['modelsTouSlabs' =>$modelsTouSlabs,]);?>

                  默认情况下,两个 div 都是隐藏的,但它们都在渲染.但我只想在选择选项基于板"或 TOU Based

                  时呈现表单

                  JS

                  $('#mdctariff-t_type').on('change', function () {if (this.value === '基于平板') {$(#slab").show();$(#tou").hide();} else if (this.value === 'TOU based') {$(#tou").show();$(#slab").hide();} 别的 {$(#slab").hide();$(#tou").hide();}});

                  注意:渲染表单后,我也在保存它

                  更新 1

                  我试图通过 ajax

                  呈现它

                  $url = Url::toRoute(['/mdctariff/_slabBased','modelsTariffSlabs'=>$modelsTariffSlabs]);doGet('$url')函数 doGet(url, params) {参数 = 参数 ||{};$.get(url, params, function(response) {//请求表单中的 url$('#slab').html(响应);//获取响应并推送到具有 id #response 的元素});}

                  参考: 如何使用 AJAX 渲染局部?Laravel 5.2

                  当我选择一个选项时,我无法查看表单.在我的 Network 选项卡中,我收到错误 Not Found (#404): Page not found..生成的 URLhttp://localhost/mdc/backend/web/mdctariff/_slabBased

                  在我的浏览器中粘贴此 URL 时,我遇到了同样的错误.我一定遗漏了一些我不知道的东西

                  如何仅在我选择一个选项时呈现我的视图?

                  任何帮助将不胜感激.

                  解决方案

                  在你的 URL 中

                  $url = Url::toRoute(['/mdctariff/_slabBased','modelsTariffSlabs'=>$modelsTariffSlabs]);

                  第一个参数应该是正确的现有路由.但是你已经以目录的形式编写了它,即 mdctafiff 文件夹,然后是 _slabBased 文件.

                  这里你需要做的是,你需要在控制器中创建一个action方法,以便你可以通过路由访问它.像MdctariffControllerpartialAction,然后在partialAction 方法的主体中,您需要调用_slabBased 视图文件.此外,您还可以参考 此处 了解 Url::toRoute().

                  I am rendering two views in my single view.

                  <?= $form->field($model, 't_type')->dropDownList([
                      '' => 'Please Select', 'Slab Based' => 'Slab Based',
                      'TOU Based' => 'TOU Based']) ?>
                  
                  <div class="showSlab" id="slab" style="display: none">
                      <?php echo $this->render('_slabBased', [
                          'modelsTariffSlabs' => $modelsTariffSlabs,
                      ]); ?>
                  </div>
                  
                  <div class="showTou" id="tou" style="display: none">
                  
                      <?php echo $this->render('_touBased', [
                          'modelsTouSlabs' => $modelsTouSlabs,
                      ]); ?>
                  </div>
                  

                  By default both div's are hidden but both of them are rendering. But I want to render the form only when I select option 'Slab Based' or TOU Based

                  JS

                  $('#mdctariff-t_type').on('change', function () {
                      if (this.value === 'Slab Based') {
                          $("#slab").show();
                          $("#tou").hide();
                  
                  
                      } else if (this.value === 'TOU Based') {
                          $("#tou").show();
                          $("#slab").hide();
                  
                  
                      } else {
                          $("#slab").hide();
                          $("#tou").hide();
                  
                      }
                  });
                  

                  Note: After rendering the form I am also saving it

                  Update 1

                  I have tried to render it via ajax

                  $url = Url::toRoute(['/mdctariff/_slabBased','modelsTariffSlabs'=>$modelsTariffSlabs]);
                  
                  doGet('$url')
                  
                  function doGet(url, params) {
                          params = params || {};
                  
                          $.get(url, params, function(response) { // requesting url which in form
                              $('#slab').html(response); // getting response and pushing to element with id #response
                          });
                      }
                  

                  Reference: How to render partial using AJAX? Laravel 5.2

                  When I selects an option I am not able to view the form. In my Network tab I am getting error Not Found (#404): Page not found.. The URL generated is http://localhost/mdc/backend/web/mdctariff/_slabBased

                  While pasting this URL at my browser I am getting the same error. I must be missing something that I don't know

                  How can render my view only when I select an option?

                  Any help would be highly appreciated.

                  解决方案

                  In your URL

                  $url = Url::toRoute(['/mdctariff/_slabBased','modelsTariffSlabs'=>$modelsTariffSlabs]);

                  The first argument should be the proper existing route. But you have written it in the form of a directory, i.e. mdctafiff folder then _slabBased file.

                  What you need to do here is, you need to create an action method in the controller so that you can access it through route. Like MdctariffController and partialAction and then in the body of partialAction method you need to call the _slabBased view file. Futher you can also take reference here for Url::toRoute().

                  这篇关于如何仅在 yii2 中选择后才呈现视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Yii 2 中将多个数据插入数据库 下一篇:Composer 抛出 [ReflectionException] Class FxpComposerAssetPlug

                  相关文章

                    <tfoot id='cwuqh'></tfoot>

                    1. <i id='cwuqh'><tr id='cwuqh'><dt id='cwuqh'><q id='cwuqh'><span id='cwuqh'><b id='cwuqh'><form id='cwuqh'><ins id='cwuqh'></ins><ul id='cwuqh'></ul><sub id='cwuqh'></sub></form><legend id='cwuqh'></legend><bdo id='cwuqh'><pre id='cwuqh'><center id='cwuqh'></center></pre></bdo></b><th id='cwuqh'></th></span></q></dt></tr></i><div id='cwuqh'><tfoot id='cwuqh'></tfoot><dl id='cwuqh'><fieldset id='cwuqh'></fieldset></dl></div>

                      <small id='cwuqh'></small><noframes id='cwuqh'>

                    2. <legend id='cwuqh'><style id='cwuqh'><dir id='cwuqh'><q id='cwuqh'></q></dir></style></legend>
                      • <bdo id='cwuqh'></bdo><ul id='cwuqh'></ul>