2016-02-19 84 views
1

我曾嘗試以下解決方案:How to set the first option on a select box using jQuery?重置一個下拉菜單,如果另一個下拉選擇改變

這些解決方案不是爲我工作,我想是因爲我的形式是不同的。我有兩個單選按鈕,並根據哪個單選按鈕被選中,顯示下拉菜單。默認情況下,兩個下拉列表都被隱藏(style="display:none")。爲了顯示和隱藏我下面簡單的代碼的下拉菜單:

$("#contactRadioButton").click(function() { 
    $("#contactDropdown").show(); 
    $("#companyDropdown").hide(); 
}); 

$("#companyRadioButton").click(function() { 
    $("#companyDropdown").show(); 
    $("#contactDropdown").hide(); 
}); 

一個代碼示例我已經試過的:

$('#contactSelect').change(function(){ 
    $('#companySelect').val(''); 
}); 


$('#companySelect').change(function(){ 
    $('#contactSelect').val(''); 
}); 

但我需要一個通聯下拉到一個值提交。現在,我可以點擊contact radio button,然後在下拉列表中選擇一個值。然後,我可以選擇company radio button並顯示其他下拉菜單,我可以選擇一個值。然後,兩個下拉菜單都會有價值。

PS。我正在使用bootstrap和bootstrap select。不知道這可以與它有任何關係。

我的完整形式的代碼(單選按鈕和下拉菜單):

<div class="form-group"> 
    <label class="control-label">Add contact or company?</label> 
    <div> 
     <div class="radio-custom radio-success radio-inline"> 
      <input id="contactRadioButton" name="contact_relatie" type="radio" value="1"> 
      <label for="contactRadioButton">Contact</label> 
     </div> 
     <div class="radio-custom radio-success radio-inline"> 
      <input id="companyRadioButton" name="contact_relatie" type="radio" value="1"> 
      <label for="companyRadioButton">Company</label> 
     </div> 
    </div> 
</div> 

<div class="form-group " id="contactDropdown" style="display:none"> 
    <label for="name">Contact:</label> 
    <select class="form-control" data-plugin="selectpicker" data-live-search="true" id="contactSelect" name="contact_id"><option value="" selected="selected">-- Select Contact --</option> // rest of options..</select> 
</div> 

<div class="form-group " id="companyDropdown" style="display:none"> 
    <label for="name">Company:</label> 
    <select class="form-control" data-plugin="selectpicker" data-live-search="true" id="companySelect" name="relation_id"><option value="" selected="selected">-- Select Company --</option> // rest of options..</select> 
</div> 
+1

你的代碼工作正常檢查https://jsfiddle.net/jnwrc5ay/29/。 –

+1

Bah。然後,這是一個奇怪的問題,我還沒有弄清楚。嘿,至少我們知道這不是上面的代碼。謝謝:) – Hardist

+0

因此,在我的表單視圖中出現的值保持選中狀態,但在幕後值重置。所以我認爲這畢竟與'selectpicker'有關。這是腳本(以防其他人遇到同樣的問題):https://silviomoreto.github.io/bootstrap-select/ – Hardist

回答

1

JS

$('#contactSelect').change(function(){ 
    $('#companySelect option:first').prop('selected', 'selected'); 
}); 


$('#companySelect').change(function(){ 
    $('#contactSelect option:first').prop('selected', 'selected'); 
}); 

HTML

<div class="form-group " id="contactDropdown" style="display:none"> 
    <label for="name">Contact:</label> 
    <select class="form-control" data-plugin="selectpicker" data-live-search="true" id="contactSelect" name="contact_id"><option value="" selected="selected">-- Select Contact --</option> // rest of options..</select> 
</div> 

<div class="form-group " id="companyDropdown" style="display:none"> 
    <label for="name">Company:</label> 
    <select class="form-control" data-plugin="selectpicker" data-live-search="true" id="companySelect" name="relation_id"><option value="" selected="selected">-- Select Company --</option> // rest of options..</select> 
</div> 
+1

OP中沒有重複的ID。 –

+0

@ZakariaAcharki我只是感到困惑。感謝更新。 –

+0

接受這個,因爲它也可以,但我的問題只是在引導選擇庫。 Thnx :) – Hardist

相關問題