2012-07-24 67 views
0

所以我有3個選擇輸入字段,我想知道是否有可能禁用其他選項後,他們已被選中。即:選擇輸入順序

<tr id="row1"> 
    <th>Row 1</th> 
    <td> 
     <select> 
      <option value="1">1</option> 
      <option value="2" selected>2 selected so 1+3 are disabled</option> 
      <option value="3">3</option> 
     </select> 
    </td> 
</tr> 

<tr id="row2"> 
    <th>Row 2</th> 
    <td> 
     <select> 
      <option value="1" selected>1 selected 2+3 are disabled</option> 
      <option value="2">2</option> 
      <option value="3">3</option> 
     </select> 
    </td> 
</tr> 

<tr id="row3"> 
    <th>Row 3</th> 
    <td> 
     <select> 
      <option value="1">1</option> 
      <option value="2">2</option> 
      <option value="3" selected>3 (you get it...)</option> 
     </select> 
    </td> 
</tr> 
+6

如果您在選擇其他選項後禁用了其他選項,用戶應如何更改其選擇? – 2012-07-24 17:34:34

+1

@MattBall我猜通過取消選中框,但這是一個奇怪的用戶體驗。 – Codeman 2012-07-24 17:36:28

+0

我相信你要找的是單選按鈕,而不是選項選擇 – Phil 2012-07-24 17:36:58

回答

2

試試這個:

$('select').change(function(){ 
    $(this).find('option:not(:selected)').prop('disabled', true) 
}) 

Demo

+0

金星! <3非常感謝你! – Robert 2012-07-24 17:53:28

2

可以綁定一個onchange事件的選擇。然後選擇哪些選項元素未被選中並禁用此選項。如果你已經熟悉的jQuery,你可以這樣來做:

$("select").bind("change", function(){ 
     $(this).children("option:not(:selected)").attr("disabled", "disabled"); 
    }); 
0

禁用其他選項將阻止用戶無論如何改變他們的選擇,所以只需使用JavaScript來刪除所有其他選項。

快速,骯髒的方式將

<select onchange='for(vari=0, l=this.options.length; i<l; i++){ if(this.options[i].selected) continue; this.remove(i); }'> 

沒有jQuery的需要。然而,我不會推薦混合使用javascript和html,除非你沒有任何其他外部javascript可以導入並將其作爲這些特定選擇語句的處理程序。