简体   繁体   English

使用jQuery动态创建选择框

[英]Create select box dynamically with jQuery

i am creating a dynamic dropdowns based on a scenario i need to. 我正在根据需要创建动态下拉列表。 i am wondering how it possible to keep the states of those added drop downs dynamically when a page is loading after submit button is clicked. 我想知道如何在单击提交按钮后加载页面时动态地保持那些添加的下拉列表的状态。

i am able to stote a variable in jquery and check whether form is submitted or not and if submitted to load back the previously dynamically added dropdowns (html select box) but even though i load them back still i have a problem of getting the previously selected values of those dynamic dropdowns. 我能够在jquery中添加一个变量,并检查是否提交了表单,以及是否提交以加载先前动态添加的下拉列表(html选择框),但是即使我重新加载它们,我仍然遇到了获取先前选择的问题这些动态下拉菜单的值。

I have created a fiddler. 我创建了一个提琴手。 http://jsfiddle.net/jBJSw/8/ http://jsfiddle.net/jBJSw/8/

My form 我的表格

<form>
    <div id="TextBoxesGroup">
        <div id="TextBoxDiv1">
            <label>Textbox #1 :</label>
            <input type="text" id="textbox1">
        </div>
    </div>
    <input type="button" value="Add Button" id="addButton">
    <input type="submit" value="submit Button" id="subButton">
</form>

Script 脚本

  $(document).ready(function () {

      var counter = 2;

      $("#addButton").click(function () {

          if (counter > 10) {
              alert("Only 10 allow");
              return false;
          }

          var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv' + counter);
          newTextBoxDiv.after().html('<label>Select #' + counter + ' : </label>' +
              '<select id="select' + counter + '" ><option value="' + "val" + ' ">"' + "desc2" + '"</option><option value="' + "val2" + ' ">"' + "desc" + '"</option><option value="' + "val3" + ' ">"' + "desc3" + '"</option>');

          newTextBoxDiv.appendTo("#TextBoxesGroup");
          counter++;
      });
  });

Appreciate if any help on finding a way to get the previously selected values after form submission. 感谢您在提交表单后寻找一种方法来获取先前选择的值的帮助。 (this is needed because when a form is submitted with errors, these selected values should be same but now it refreshes and comes to default. also in a view scenario also this should be preloaded) (这是必需的,因为在提交带有错误的表单时,这些选择的值应该相同,但是现在它会刷新并成为默认值。在视图场景中,也应该预先加载)

You will have to send back the selected value to the page from server side in the querystring or post header. 您必须将所选值从服务器端的querystring或post标头发送回页面。 then select the option accordingly in jquery on the base of the the value sent back. 然后根据返回的值在jquery中相应地选择选项。

Sorry for getting late have trouble in my work. 很抱歉迟到了我的工作。 Here's a possible solution for your problem. 这是您问题的可能解决方案。

DOM: DOM:

<form id="myform" method="post"> <!--adding id attribute-->
    <div id="TextBoxesGroup">
        <div id="TextBoxDiv1">
            <label>Textbox #1 :</label>
            <input type="text" id="textbox1">
        </div>

        <!--check if POST is established-->
        <?php if($_POST): ?>
            <?php if(count($_POST['mySelect']) < 0 ): ?>

            <?php

               //Recreate your select DOM
               $arr = $_POST['mySelect'];
               foreach($arr as $ind => $val){

                  echo '<select>';
                  echo '<option val="val" ' . ($val == "val" ? "selected" : "") . '>desc2</option>';
                  echo '<option val="val2" ' . ($val == "val2" ? "selected" : "") . '>desc</option>';
                  echo '<option val="val3" ' . ($val == "val3" ? "selected" : "") . '>desc3</option>';
                  echo '</select>';
               }

            ?>                


            <?php endif; ?>
        <?php endif; ?>
    </div>
    <input type="button" value="Add Button" id="addButton">
    <input type="submit" value="submit Button" id="subButton">
</form>

SCRIPT: 脚本:

 $(document).ready(function () {

      var counter = 2;

      $("#addButton").click(function () {

          if (counter > 10) {
              alert("Only 10 allow");
              return false;
          }

          var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv' + counter);
          newTextBoxDiv.after().html('<label>Select #' + counter + ' : </label>' +
              '<select id="select' + counter + '" ><option value="' + "val" + ' ">"' + "desc2" + '"</option><option value="' + "val2" + ' ">"' + "desc" + '"</option><option value="' + "val3" + ' ">"' + "desc3" + '"</option>');

          newTextBoxDiv.appendTo("#TextBoxesGroup");
          counter++;
      });

     //adding hidden inputs
     $('#myFORM').on('submit', function(){

      $('#myFORM select').each(function(){
         slct = $('<input type="hidden" name="mySelect[]" value="' +  $(this).val() +'">');
         $('#myFORM').append(slct);
      });

  });
  });

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

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