如何将所选选项POST到控制器

时间:2015-09-10 02:46:09

标签: javascript jquery model-view-controller drop-down-menu selectlistitem

我创建了以下下拉列表以及将输出“block”的条件检查 - >与选项关联的详细信息。见例如:

function val(ddbox) {

         //NEW
        if ( ddbox.options[ddbox.selectedIndex].value == "Others"){
                document.getElementById("extradiv").style.display = "block";
        }else{
                document.getElementById("extradiv").style.display = "none";
        }
         if( ddbox.options[ddbox.selectedIndex].value != "Others"){
                document.getElementById("NumberChosen").style.display = "block";
        }else{
                document.getElementById("NumberChosen").style.display = "none";
        }
}
 <li class="bigfield">
                        <select name ="ddbox" id="ddbox" onchange = "val(this)">
                        <option value ="0"> Select Number..</option>
                        <option value ="Option 1"> 1</option>
                        <option value ="Option 2"> 2</option>
                        <option value ="Option 3"> 3</option>
                        <option value = "Others"> Others</option>
                        </select>
                </li>


                <!--when user select "Others"-->
                <div id = "extradiv" style ="display:none">
                <li class="bigfield"><input placeholder="Enter your Num" type="text" name="OthersNum" id="OthersNum"/></li>
                <li class="bigfield"><input placeholder="Number Characteristics" type="text" name="NumberOthersCharacteristics" id="NumberOthersCharacteristics"/></li>
                <li class="bigfield"><input placeholder="why do you like this number" type="text" name="NumberOthersLikefactor" id="NumberOthersLikefactor"/></li>
                 </div>

                 <!--when user select other options-->
                <div  id = "NumberChosen" style ="display:none" >
               <li class="bigfield"><input value="NumChosen" type="text" name="NumChosen" id="NumChosen" readonly></li>
                <li class="bigfield"><input value="Num Chosen Characteristic" type="text" name="NumChosenCharactersitics" id="NumChosenCharactersitics" readonly/></li>
                <li class="bigfield"><input value="Num Chosen Likes" type="text" name="NumChoseLikes" id="NumChoseLikes" readonly/></li>
                 </div>

我已经选择了每个选项来引用详细信息,当用户点击“其他”时,它将引用输入字段。因此,字段表单在MVC框架的视图内完成。

为了确保在提交时,值正确地POST到控制器代码,我在控制器代码中做了以下错误日志,以检查从表单捕获的值。如下所示的error_logs:

       error_log(date("Y-m-d H:i:s")."_ipad,Num: ".$_POST['OthersNum']."\n",3,"/var/tmp/*/*.log");
       error_log(date("Y-m-d H:i:s")."_-ipad, NumChar: ".$_POST['NumberOthersCharacteristics']."\n",3,"/var/tmp/*/*.log");
       error_log(date("Y-m-d H:i:s")."_-ipad, NumLikes: ".$_POST['NumberOthersLikefactor']."\n",3,"/var/tmp/*/*.log");

       error_log(date("Y-m-d H:i:s")."_-ipad,NumberChosen: ".$_POST['NumChosen']."\n",3,"/var/tmp/*/*.log");
       error_log(date("Y-m-d H:i:s")."_-ipad, NumChosenChar: ".$_POST['NumChosenCharacteristics']."\n",3,"/var/tmp/*/*.log");           error_log(date("Y-m-d H:i:s")."_-ipad, NumChosenLikes: ".$_POST['NumChoseLikes']."\n",3,"/var/tmp/*/*.log");

现在,这是我面临的主要问题,当我运行以下终端命令时,错误日志将始终显示:'tail -f * .log',例如,如果我要选择“其他”在选项列表中并键入相应的字段,并提交表单,值将POST到控制器代码,错误日志不仅会显示“OthersNum”的值,还会显示NumChosen的详细信息。这不是我想要的结果,我正在查看错误日志中仅发布为“其他”选项输入的详细信息,并且不显示与其他选项相关的详细信息

因此,显示了冗余error_log,据我所知,表单中的所有值都被POST到控制器。因此,我如何编辑现有代码,以便只将选定的所选选项张贴到控制器上?

1 个答案:

答案 0 :(得分:0)

我认为你的问题来自if语句,你不想对同样的条件使用多个if语句,你的if语句应该是,

function val(ddbox) {
        if ( ddbox.options[ddbox.selectedIndex].value == "Others"){
                document.getElementById("extradiv").style.display = "block";
               document.getElementById("NumberChosen").style.display = "none";
        }else{
                document.getElementById("extradiv").style.display = "none";
                document.getElementById("NumberChosen").style.display = "block";
        }
}

对类似的条件使用单个if语句,希望这有效...