2011-03-21 74 views
6

我是jQuery的新手,并认为我会使用它的按钮集而不是我的应用程序上的某些单选按钮。这里的文档:http://jqueryui.com/demos/button/#radiojQuery按钮组更改

当一组按钮更改值时,如何向事件添加处理程序?

这里是我试过的代码片段:

$('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的无知并不完全有助于这种情况。

任何帮助将不胜感激。谢谢。

回答

6

哈!问题解决了。我一整天都在黑客攻击,而且在我面前。

我需要改变我是如何选择的元素添加到:

$('#tod_quant') 
+0

当我写同样的东西时,你的答案闪现了! – 2011-03-21 03:27:29

9

有在jQuery的UI按钮没有change事件。

你应该听更改或点击下面的单选按钮的事件:

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

+2

虽然这是真的,但您可以听取应用于按钮组的更改事件。看起来像一个jQuery复活节彩蛋,但这工作正常:'$('#tod_quant')。buttonset()。change(function(){// do stuff});' – jdp 2016-05-23 15:55:43

-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文件或不。

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 
} 

没有别的办法!我测试了几种方法,最后决定去做这件事

+0

作为使用动态创建复选框的附加说明,您必须在调用buttonset()之后包含事件处理程序。如果您从ajax获取字段,则必须在每次更新时完成。很明显,当你考虑它时,我花了相当多的时间来检查拼写错误和语法,然后才意识到我在不存在的东西上创建了一个处理程序。 – carruthd 2013-11-01 14:39:44