2016-02-11 66 views
0

我想單擊按鈕時添加一個按鈕到列將當前頁面上的所有其他下拉項目設置爲所做的選擇。點擊更新所有表格下拉列表項目

JSFiddle

<select class="select2 select2-offscreen" id="id[0]" name="id[0]"> 
<option value="not received">Not Received</option> 
<option value="received">Received</option> 
</select> 

<select class="select2 select2-offscreen" id="id[1]" name="id[1]"> 
<option value="not received">Not Received</option> 
<option value="received">Received</option> 
</select> 

<select class="select2 select2-offscreen" id="id[2]" name="id[2]"> 
<option value="not received">Not Received</option> 
<option value="received">Received</option> 
</select> 

<button class="go-btn" type="submit">Update All</button> 

JS:

$('.go-btn').click(function() { 

     $("select option").each(function() { 
      //how to update the dropdow? 
     }); 
}); 
+0

你是什麼意思與「選擇」?我不確定你想要什麼 – DarkAjax

+0

「選擇作出」,這意味着如果用戶選擇「未收到」並點擊按鈕,則所有下拉列表應被選擇爲「未收到」,並且對於「已收到」和用戶點擊按鈕,然後所有的下拉列表應改爲「收到」希望這是明確的。 –

回答

3

這是你在找什麼? https://jsfiddle.net/zmbagh9h/3/

var lastSelectedValue = 'not received'; 

$('select').change(function() { 
    lastSelectedValue = this.value; 
}); 

$('.go-btn').click(function() { 
    $('select').val(lastSelectedValue); 
});