简体   繁体   中英

Dynamically add Dropdown and Text Box in same row Javascript

<div class="row">
    <div class="col-md-3" style="padding-top: 10px">
    <class="tg-83xr"> <select id="year" name="year">

    <script>
        var myDate = new Date();
        var year = myDate.getFullYear();

        for(var i = 1900; i < year+1; i++){
            document.write('<option  value="'+i+'">'+i+'</option>');}
    </script></select>
    </div>

    <div class="col-md-3" style="padding-top: 10px">    
        <input type="text" class="form-control" id="d000" enabled size = "30">      
    </div>

    <div class="col-md-3" style="padding-top: 10px">    
    <input type="text" class="form-control" id="d001" enabled size ="30" >      
    </div>
</div>

<script>
    $(document).ready(function(){
    $("#num").click(function () {
    $('#form').append("<div class='row'><div class='col-md-3' style='padding top: 10px'><select class=tg-83xr id='year' > </select></div><div class='col-md-3' style='padding-top: 10px'>   <input type='text' class='form-control' id='d000'>    </div><div class='col-md-3' style='padding-top: 10px'><input type='text' class='form-control' id='d001'></div></div>")});});
</script>

<div id="form">
</div>

<br>
<button id="num"> ADD </button> To add another click here

Here, i am not able to add options in the dropdown list which contains a list of years(1900-2016) in the .append() function.

Better don't use document.write. You can use append() method to add the options to select.

<select id="year" name="year">
  </select>
<script>
var myDate = new Date();
var year = myDate.getFullYear();

for (var i = 1900; i < year + 1; i++) {
  $("#year").append('<option  value="' + i + '">' + i + '</option>');
}
</script>
var optionsString = "";

for( var i = 1900; i <= 2016; i++ ) // OR for( var i = 1900; i <= new Date().getFullYear(); i++ )
{
    optionsString += '<option value="'+i+'">'+i+'</option>';
}

 $('#form').append("<div class='row'><div class='col-md-3' style='padding top: 10px'><select class=tg-83xr id='year' >"+optionsString+"</select></div><div class='col-md-3' style='padding-top: 10px'>   <input type='text' class='form-control' id='d000'>    </div><div class='col-md-3' style='padding-top: 10px'><input type='text' class='form-control' id='d001'></div></div>")});});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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