简体   繁体   English

Yii2 Ajax .post从下拉列表到控制器的视图列表以及接收数据时的一些操作

[英]Yii2 Ajax .post to controller from dropdownList of view and some action upon receiving data

I have a DropDown list. 我有一个下拉列表。 I've written the code but it's not working. 我已经编写了代码,但是没有用。 Please help me to fix it: 请帮助我修复它:

echo $form->field($model, 'Adrop')->dropDownList(
    [
        '' => 'Please Choose',
        '1' => 'item 1',
        '2' => 'item 2'
    ],
    [
        'onchange' => '$.post(Yii::$app->urlManager->createUrl . "users/A_action"), function(data) {
            $("#test_div").html(data)
        }'
    ]
);

Also I want to send selected data, and don't know where to write it. 我也想发送选定的数据,不知道在哪里写。

In the Controller I have this action 在控制器中,我有此操作

 public function actionA_action() {
     $data = "TTT";
     return $data;
 }

Now when I select something in the DropDown list, nothing happens in my test_div :( 现在,当我在“ DropDown”列表中选择某项时, test_div没有任何test_div :(

UPDATE Thanks to Mihai P. now I'm using this code 更新感谢Mihai P.现在我正在使用此代码

<?php
      echo   $form->field($model, 'Adrop')->dropDownList(
          [''=>'Please Choose','1'=>'item 1','2'=>'item 2'],
          [
          'onchange'=>'$.post( "'.Yii::$app->urlManager->createUrl(["users/A_action"]).'",function(data){
                $("#test_div").html( data )
                }']);
        ?>

The HTML is formed in the following way HTML通过以下方式形成

<select id="A-adrop" class="form-control" name="A[Adrop]" onchange="$.post( &quot;/users/A_action&quot;,function(){
                $(&quot;#test_div&quot;).html( data )
                }">
<option value="">Please Choose</option>
<option value="1">item 1</option>
<option value="2">item 2</option>
</select>

But when I choose something in debug this string is highlighted 但是当我在调试中选择某些内容时,该字符串会突出显示

 <option value="2">item 2</option>

and there is one error saying 而且有一个错误说

Uncaught SyntaxError: Unexpected token }

Final UPDATE 最终更新

I've added one closing bracket on the last string of this code there are two of them closing now as you can see, and that was the problem. 我在此代码的最后一个字符串上添加了一个右括号,如您所见,其中有两个现在正在关闭,这就是问题所在。 Semicolumn also will be a plus, but I've tested code works without it OK. 分号也将是一个加号,但我测试过的代码在没有它的情况下可以正常工作。 problem was in closing bracket. 问题在于右括号。

 'onchange'=>'$.post( "'.Yii::$app->urlManager->createUrl(["users/A_action"]).'",function(data){
                    $("#test_div").html( data );
                    })']);

well I am sure you have a javascript error. 好吧,我确定您遇到了JavaScript错误。 You should actually have a lot of them too. 实际上,您也应该有很多。

You are doing this 你正在这样做

'onchange' => '$.post(Yii::$app->urlManager->createUrl . "users/A_action"), function(data) {
            $("#test_div").html(data)
        }'

You are not actually calling Yii::$app->urlManager->createUrl you are just using it as a string. 您实际上并没有在调用Yii :: $ app-> urlManager-> createUrl,而是将其用作字符串。

You probably need something like 您可能需要类似

...
['onchange' => '$.post("'.Yii::$app->urlManager->createUrl(["users/A_action"]).'", function( data ) {
      $("#test_div").html( data );
     })']);

Simple add a JS block, it is much more clean: 简单地添加一个JS块,它就更干净了:

<?php $this->registerJs("
    jQuery(function($){
        $('select[name=Adrop]').on('change', function(){
             $.post(Yii::$app->urlManager->createUrl . 'users/A_action'), 
             function(data) {
                   $('#test_div').html(data)
             }
        });
    });");?>

Just go through these codes, you may understand the working 只需通过这些代码,您就可以了解其工作原理

    $(document).ready(function () {
        $(document.body).on('change', 'yourid', function () {   
            var val = $('yourid': selected').val(); 
            if(val == 'I' ) {
                something
            } else if(val == 'B' ){
                something
            }
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM