2011-11-18 54 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