2017-09-27 18 views
0

我需要创建一个添加按钮,每次用户按下它时,都会创建一个新的下拉列表,并且选定的值不会在新的dpl中。第一个下拉列表是从数据库中获取数据(这是完成的)。添加按钮,将创建新的下拉列表,并从新的dpl中删除选定的值

我试过这样[示例] [1]但它不起作用!有小费吗?

而且,这是惯例所以不需要验证等等等等

HTML代码

<div id="services-D" class="oneField" >     
        <label id="services-L" for="services" class="label preField reqMark" style="width: 100px; min-width:0">Services</label><br> 
        <select id="c_service" name="c_service" class="required dynamic-select" style="width:170px" > 
         <option value="">Select Service...</option> 
         <?php 
          $res=mysqli_query($dbc,"Select t1 t2 Where t4s = 'Y'"); 
          while ($row = mysqli_fetch_array($res)) { ?> 
          <option value = <?php echo $row['invoice_code']?> > <?php echo $row['t1'] . ": " . $row['t2'];}?> </option> 
         ?> 
        </select> 

       </div> 
<button type="button" id="btn_add_service" style="">Add Service</button> 

jQuery的

<script> 
$('#btn_add_service').on('click', function() { 
    selected = $('#c_service').prop('selectedIndex'); 
    if (selected != 0 && $('#c_service').find('option').length != 2){ 
    newselector = $('<select id="c_service"></select>').insertAfter('#c_service'); 
    $('#c_service').find('option').each(function(index,item) { 
     if (index != selected){ 
     $(newselector).append(item); 
     } 
    }); 
    } 
}); 
</script> 
+0

什么'选择value'?请分享一些HTML,JavaScript来举例说明问题。 –

+0

立即查看@AlanLarimer – noel293

回答

1

不知道这是什么exacly你正在寻找,但我试图根据你的描述做出来。希望this link可以帮助你。

HTML:

<select onchange="NewSelection(this);"> 
    <option>Select an option</option> 
    <option>Option 1</option> 
    <option>Option 2</option> 
    <option>Option 3</option> 
    <option>Option 4</option> 
    <option>Option 5</option> 
</select> 

的JavaScript/JQuery的:

function NewSelection(selector){ 
    selected = $(selector).prop('selectedIndex'); 
    if (selected != 0 && $(selector).find('option').length != 2){ 
    newselector = $('<select onchange="NewSelection(this);"></select>').insertAfter(selector); 
    $(selector).find('option').each(function(index,item) { 
     if (index != selected){ 
     $(newselector).append(item); 
     } 
    }); 
    } 
} 
+0

谢谢!这个工作得很好!我将不得不修改它,以便它将打印一个在另一个之下! – noel293

+0

实际上使用这个代码的按钮使事情变得复杂一点!它确实显示了一个新的dpl列表,但只有一个..我编辑了我的问题,如果你可以看看 – noel293

相关问题