2015-10-19 175 views
-1

我想讓所選的單選按鈕隱藏所有未選中的單選按鈕?我想的方法是首先獲取選定的單選按鈕,然後取消選擇其他每個按鈕的ID。但除此之外還有其他方法嗎? (我想獲取每個ID和取消選擇是有點麻煩)顯示所選的單選按鈕並隱藏所有其他

這裏是我的html

<form> 
     <group class="inline-radio"> 
     <div> 
      <input id="opt1" type="radio" name="title"> 
      <label>opt1</label> 
     </div> 
     <div> 
      <input id="opt2" type="radio" name="title" checked> 
      <label>opt2</label> 
     </div> 
     <div> 
      <input id="opt3" type="radio" name="title"> 
      <label>opt3</label> 
     </div> 
     <div> 
      <input id="opt4" type="radio" name="title"> 
      <label>opt4</label> 
     </div> 
     <div> 
      <input id="opt5" type="radio" name="title"> 
      <label>others</label> 
     </div> 
     </group> 

</form> 

該項目是codepen http://codepen.io/flyingboy007/pen/ojoVWe

+2

您的使用者應該改變自己在這種情況下選擇的選項?另外,請在你的問題中發佈你的JavaScript(和你的[MCVE])*這裏*;不要指望人們爲了幫助你而喜歡互聯網。順便說一句,沒有''元素;您可能正在考慮 - 或者有意使用 - ['

'](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/fieldset)。此外,「
+0

我沒有它..我只需要一些建議基於提供的HTML。所以我可以寫jQuery的一部分 – Abhilash

+0

然後我認爲你有網站倒退;你應該嘗試實施一個解決方案,然後*然後*尋求幫助,如果試圖解決方案不起作用或以某種方式失敗(解釋它*應該*做什麼,它*不做*做什麼等)。 –

回答

1

你可以hi德使用未選擇的收音機: -

$('input[type="radio"]').not(':checked').hide(); 

,您可以隱藏未選擇的無線電標籤的使用

$('input[type="radio"]').not(':checked').siblings('label').hide(); 

但應在標籤上使用for

編輯

更具體到該組,使用: -

$('group.inline-radio').find('input[type="radio"]').not(':checked').hide(); 

$('group.inline-radio').find('input[type="radio"]').not(':checked').siblings('label').hide(); 

或鏈狀的無線電和標籤

$('group.inline-radio').find('input[type="radio"]').not(':checked').hide().siblings('label').hide(); 
+0

謝謝..確切地說,我在找什麼.. – Abhilash

+0

只是一個快速的疑問..我希望所選的單選按鈕浮動到左側(在第一個單選按鈕的位置),同時隱藏其他人。有沒有任何jQuery的方法可用於? – Abhilash

+0

使用css - float:left;在輸入/標籤周圍的容器div具有使它們不會離開的寬度。在標籤上設置寬度而不是容器。 – BenG

1

嘗試:

$('input ').on('click',function(){ 
    $('input').not($(this)).hide(); 
}); 

在點擊全選輸入除選定的一個並隱藏tham

+0

請花時間來解釋這是如何工作的,以便OP和未來的訪問者可能能夠從中學到一些有用的東西。 –

相關問題