2011-11-18 53 views
0

我有兩組按鈕,我喜歡按鈕的作用像單選按鈕,所以只能選擇一個按鈕。如何使按鈕充當與jQuery的單選按鈕?

例如:

<div id="top"> 
    <div class="span2">       
    <input type=button value="10" id="top_A" name="top_A" onclick="yourPick(this)"/> 
    </div> 
    <div class="span2"> 
    <input type=button value="20" id="top_B" name="top_B" onclick="yourPick(this)"/> 
    </div> 
</div> 
<div id="bottom"> 
    <div class="span2">       
    <input type=button value="30" id="top_C" name="top_C" onclick="yourPick(this)"/> 
    </div> 
    <div class="span2"> 
    <input type=button value="40" id="top_D" name="top_D" onclick="yourPick(this)"/> 
    </div> 
</div> 

所以,你可以在DIV底部選擇在頂部只是按鈕10或20比30或40。

有什麼建議嗎?

PS:

function yourpick(pick){ 
    var pValue= pick.value; 
    alert(pValue); 
    $('#topPick').val(pValue); 
    $('#bottomPick').val(pValue); 
} 
+7

如何http://jqueryui.com/demos/button/#radio從jQuery UI的 – Matt

+3

哪些錯誤?有單選按鈕? – simonlchilds

+0

@csharpsi,界面是基於按鈕,所以對於用戶我喜歡使用按鈕。馬特看起來很有前途我要檢查它 – alex

回答

0

即使我同意user832715使用按鈕的禁用屬性。

$( 「#selectButton1DivA」)ATTR( 「已禁用」,假/真「);

4

我的onclick移除您examble屬性,但你可以使用組中禁用所選按鈕屬性,使所有其他人。這就是他們的行爲如同單選按鈕。

$(document).ready(function() { 

    handleGroup('#top .span2 input'); 
    handleGroup('#bottom .span2 input'); 

}); 


function handleGroup(selector) { 
    var inputs = $(selector); 

    inputs.live('click', function() { 

     //this returns id if needed 
     //alert($(this).attr('id')); 

     inputs.attr('disabled',''); 

     $(this).attr('disabled','disabled'); 

    }); 
} 
+0

我也在嘗試你的建議,但按鈕似乎並沒有切換。我的意思是,當我選擇其中一個#top按鈕時,我無法選擇另一個。 – alex