JQuery: Dynamically create select Tag

后端 未结 4 567
一个人的身影
一个人的身影 2020-12-16 12:40

I am using JQuery to dynamically (based on user choice) create tag. User enters require options in a text box and my code creates select tag of it. Script is:



        
相关标签:
4条回答
  • 2020-12-16 13:19
    <!-- Create Dropdown -->
    /* 1- First get total numbers of SELECT tags used in your page to avoid elements name/id issues.
     * 2- Get all OPTIONS user entered with comma separator into a text box.
     * 3- Split user OPTIONS and make an array of these OPTIONS.
     * 4- Create SELECT code.
     * 5- Appent it into the temp div (a hidden div in your page).
     * 6- Then get that code.
     * 7- Now append code to your actual div.
     * 8- Filnally make empty the temp div therfore it will be like a new container for next SELECT tag.
     */
    
    total = $("select").size() + 1;                                         // 1-
    var myoptions = $("#myoptions").val();                                  // 2-
    var data = myoptions.split(',');                                        // 3-
    var s = $("<select id=\""+total+"\" name=\""+total+"\" />");            // 4-
    for(var val in data) {
        $("<option />", {value: val, text: data[val]}).appendTo(s);
    }
    s.appendTo("#tempselect");                                              // 5-
    var getselectcode = $("#tempselect").html();                            // 6-
    $("#msj_form").append(appendLabel+"<td>"+getselectcode+"</td></tr>");   // 7-
    $("#tempselect").html('');                                              // 8-
    
    <div style="display:none;" id="tempselect"></div>                       // Temp div
    
    0 讨论(0)
  • 2020-12-16 13:21
    var html = '<tr><td><label for="select" style="text-transform:   none;">Label_name</label></td>'
                +'<td><select name="select" id="">'
                +'<option>Choose number..</option>'
                +'<option value="0">1</option>'
                +'<option value="1>2</option>'
                +'<option value="2">3</option>'
                +'</select></td></tr>';
    

    Let's suppose, the id of table = table_id

    $('#table_id').append(html);
    $('#table_id').trigger('create');
    

    What i get is, if you don't call trigger(), the design of your select looks completely messed up..

    0 讨论(0)
  • 2020-12-16 13:23

    Slight amendment to the answer by Ravi - appending each element one by one is a surprisingly high cost operation.

    var s = $("<select id=\"selectId\" name=\"selectName\" />");
    var opts = [];
    for(var val in data) {
        opts.push( $("<option />", {value: val, text: data[val]}));
    }
    
    opts.appendTo(s);
    
    0 讨论(0)
  • 2020-12-16 13:25
    var s = $("<select id=\"selectId\" name=\"selectName\" />");
    for(var val in data) {
        $("<option />", {value: val, text: data[val]}).appendTo(s);
    }
    

    after the for loop, wrap all the content with TableRow and Cells like this , Jquery Wrap()

    $(s).wrap('<table><tr><td></td></tr></table>');
    
    0 讨论(0)
提交回复
热议问题