获取ButtonSet中的单击元素

5 jquery

我想知道点击了哪个元素,以便我可以更改其CSS类.这是代码:

<script type="text/javascript">
      $(function() {
       $("#radio").buttonset();
      });
</script>
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

Bru*_*oLM 13

jQuery事件this作为触发事件的对象返回.所以你只需要检查一下this.

拥有这组元素

<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>
Run Code Online (Sandbox Code Playgroud)

选择所有无线电元素:

$("#radio :radio")
Run Code Online (Sandbox Code Playgroud)

然后绑定一个事件:

.click(function(e) { });
Run Code Online (Sandbox Code Playgroud)

检索元素this:

$("#radio :radio").click(function(e) {
    var rawElement = this;
    var $element = $(this);

    /* Do stuff with the element that fired the event */
});
Run Code Online (Sandbox Code Playgroud)

关于jsFiddle的示例.

你可以在这里找到操作元素类的函数.


gsa*_*lis 5

您还可以使用以下方法从按钮组中检索选定的单选按钮:

$j("#radioset :radio:checked")
Run Code Online (Sandbox Code Playgroud)

然后用元素做你想做的事......


Bab*_*ker 0

$("#radio :radio").click(function(){
    //alert($(this).attr("id"));
});
Run Code Online (Sandbox Code Playgroud)