2017-09-27 23 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

相關問題