jQuery按钮组更改

时间:2011-03-21 03:21:22

标签: jquery jquery-ui jquery-ui-button

我是jQuery的新手,并且认为我会在我的应用程序中使用它的buttonset而不是一些单选按钮。这里的文档:http://jqueryui.com/demos/button/#radio

当按钮组改变值时,如何为事件添加处理程序?

以下是我尝试过的代码片段:

$('input.tod_quant').change(function() {
    alert('TEST');
});

然后在HTML中:

<span id="tod_quant" class="buttonset">
        <input type="radio" id="tod_quant5" name="tod_quant" value="5" /><label for="tod_quant5">5-Minute</label>
        <input type="radio" id="tod_quant60" name="tod_quant" checked="checked" value="60" /><label for="tod_quant60">60-Minute</label>
        </span>

“改变”事件永远不会发生。甚至有变化事件吗?我怎样才能做到这一点?此外,是否有任何文档的例子? http://jqueryui.com有很多例子,我找不到任何一个例子可以显示任何事件。我想我对jQuery的无知并不能完全帮助这种情况。

任何帮助都将非常感激。谢谢。

4 个答案:

答案 0 :(得分:9)

jQuery-UI中的按钮没有change事件。

您应该听取更改或点击基础单选按钮的事件:

http://jsfiddle.net/marcosfromero/kr2Xc/

答案 1 :(得分:6)

哈!问题解决了。我整天都在乱砍这个,而且它就在我面前。

我只需要改变我选择元素的方式:

$('#tod_quant')

答案 2 :(得分:2)

我遇到了这个问题,我解决了以下问题: 我为标签创建了一个事件处理程序:

$('.label-class').click(LabelClicked);
$('.input-class').change(InputChanged);
function LabelClicked() {
    var input = $('#' + $(this).attr('for'));
    if(input) 
        input.trigger('change'); 
    return true;
}
function InputChanged() {
    //do your work with this event
}
别无他法!我测试了几种方法,最后决定这样做

答案 3 :(得分:-1)

    <script>
    $(function() {
        $( "#radio" ).buttonset();
    });
    </script>



<div class="demo">

<form>
    <div id="radio">
        <input type="radio" id="radio1" name="radio" /><label for="radio1">Choice 1</label>
        <input type="radio" id="radio2" name="radio" checked="checked" /><label for="radio2">Choice 2</label>
        <input type="radio" id="radio3" name="radio" /><label for="radio3">Choice 3</label>
    </div>
</form>

使用此代码并检查是否包含jquery.js和jqueryui.js文件。

相关问题