1

我认为(_form.php)有这个。

<div>
        <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#pilih_dulu").change(function(){
                    $(this).find("option:selected").each(function(){
                        if($(this).attr("value")=="1"){
                            $(".box").not(".1").hide();
                            $(".1").show();
                        }

                        else if($(this).attr("value")=="2"){
                            $(".box").not(".2").hide();
                            $(".2").show();
                        }

                        else if($(this).attr("value")=="3"){
                            $(".box").not(".3").hide();
                            $(".3").show();
                        }

                        else{
                            $(".box").hide();
                        }
                    });
                }).change();
            });
        </script>
    </div>

    <div class = "col-lg-4">
        <?= $form->field($model, 'kat_id')->label(true)->dropDownList(
            ArrayHelper::map(TblKategori::find()->all(),'kat_id','kat_kategori'), ['id' => 'pilih_dulu']
        ) ?>
    </div>

    <div class="1 box col-lg-4">
        <?= $form->field($model, 'sok_id')->textInput(['value' => 1]) ?>
    </div>

    <div class="2 box col-lg-4">
        <?= $form->field($model, 'sok_id')->textInput(['value' => 2]) ?>
    </div>

    <div class="3 box col-lg-4">
        <?= $form->field($model, 'sok_id')->textInput(['value' => 2]) ?>
    </div>

问题是它没有存储正确的值。它似乎总是在最后一个盒子中存储价值。

它的工作原理是这样的:

  1. 如果从下拉列表中选择值 1,则应将 sok_id 的值存储在“1 框”中。

  2. 如果从下拉列表中选择了值 2,它应该将 sok_id 的值存储在“2 框”中。

  3. 如果从下拉列表中选择值 3,它应该将 sok_id 的值存储在“3 框”中。

请帮我。谢谢你

4

1 回答 1

1

如果我猜对了,您可以通过简单的方式做到这一点:

<div class = "col-lg-4">
   <?= $form->field($model, 'kat_id')->label(true)->dropDownList(
      ArrayHelper::map(TblKategori::find()->all(),'kat_id','kat_kategori'), ['id' => 'pilih_dulu', 'onchange' => 'if($(this).val() == 1) {
                        $("#'.Html::getInputId($model, 'sok_id').'").val($(this).val());
                    }
                    else if($(this).val() == 2) {
                        $("#'.Html::getInputId($model, 'sok_id').'").val($(this).val());
                    } else if($(this).val() == 3){
                        $("#'.Html::getInputId($model, 'sok_id').'").val($(this).val());
                    }'
   ']) ?>
</div>

 <div class="box col-lg-4">
    <?= $form->field($model, 'sok_id')->textInput() ?>
</div>

不需要 3 个字段,并且在必要时添加show()和方法。hide()

于 2016-06-27T08:48:44.647 回答