doulipi3742
2016-11-29 08:27
浏览 59
已采纳

将javascript值传递给Yii2中的表单操作URL

I'm trying to put a value from javascript into a form action in Yii2 is it possible? More specifically I need to make a URL change for each option that is selected in a dropdownList.

form in views/site.php

$form = ActiveForm::begin([
       'id' => 'form',
       'method' => 'POST',
       'action' => Url::to(['programas/'.Tours::findOne(['pk' => ])->programa]),
]); 

<?= $form->field(new \app\models\Tours(), 'nombre')->dropDownList([],
    [
        'prompt' => 'Programa',
        'id' => 'child1_child2',
        'onchange' => 'updateValue(this.value)',
   ]
)->label(false); ?>

Js file

    function updateValue(val){
  x = document.getElementById("test").value;
  // document.getElementById("form").action = "programas/";
}

So, I rescue the value from the selected option with JS but I need to put it in here 'pk' => 'value'

where value is the #child1_child2 selected option value.

'action' => Url::to(['programas/'.Tours::findOne(['pk' =>  ])->programa])

Thanks for the help.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将javascript中的值放入Yii2中的表单操作 它可能吗? 更具体 我需要为在dropdownList中选择的每个选项进行URL更改。

表单在views / site.php中

  $  form = ActiveForm :: begin([
'id'=&gt;'form',
'method'=&gt;'POST',
'action'=&gt; Url :: to(['programas /'  .Tours :: findOne(['pk'=&gt;]) - &gt; programa]),
]);  
 
&lt;?= $ form-&gt;字段(new \ app \ models \ Tours(),'nombre') - &gt; dropDownList([],
 [
'prompt'=&gt;'Programa'  ,
'id'=&gt;'child1_child2',
'onchange'=&gt;'updateValue(this.value)',
 
 
) - &gt;标签(false);  ?&gt; 
   
 
 

Js文件

  function updateValue(val){
x = document.getElementById(“  test“)。value; 
 // document.getElementById(”form“)。action =”programas /“; 
} 
   
 
 

所以,我救了 来自JS的所选选项的值,但我需要把它放在这里'pk'=&gt; 'value'

其中value是#child1_child2选择的选项值。

 'action'=&gt;  Url :: to(['programas /'。Tours :: findOne(['pk'=&gt;]) - &gt; programa])
   
 
 

谢谢你 帮助。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drt41563 2016-11-29 11:29
    已采纳

    You can use one of the following approach with some modification:

    //pk => programa
    $data = [
      1 => 'programa1',
      2 => 'programa2',
      3 => 'programa3',
    ];
    

    1) Use JS

    <?= $form->field(new \app\models\Tours(), 'nombre')->dropDownList($data, ['prompt' => 'Programa'])->label(false); ?>
    

    JS

    $this->registerJs('
    $("#dropdownID").change(function() {
        var text = $("#dropdownID option:selected").text();
        $("#formID").attr("action", "/pathtoproject/programas/" + text);
    });
    ', \yii\web\View::POS_END);
    

    2) Use Ajax Call

    <?= $form->field(new \app\models\Tours(), 'nombre')->dropDownList($data, [
       'prompt' => 'Programa',
        'onchange'=> '$.get( "'.Url::toRoute('get-action').'", { id: $(this).val() } )
           .done(function( data ) {
               $("#formID").attr("action", data); 
           } 
        );'
    ])->label(false); ?>
    

    Controller

    public function actionGetAction($id)
    {
       $name = Tours::findOne(['pk' =>  $id])->programa];
       echo \yii\helpers\Url::to(['programas/'.$name]);
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题