简体   繁体   English

javascript jquery单选按钮单击

[英]javascript jquery radio button click

I have 2 radio buttons and jquery running. 我有2个单选按钮和jquery运行。

<input type="radio" name="lom" value="1" checked> first
<input type="radio" name="lom" value="2"> second

Now, with a button I can set onClick to run a function. 现在,使用按钮我可以设置onClick来运行一个功能。 What is the way to make radio buttons run a function when I click on one of them? 当我点击其中一个时,单选按钮运行功能的方法是什么?

You can use .change for what you want 您可以根据需要使用.change

$("input[@name='lom']").change(function(){
    // Do something interesting here
});

as of jQuery 1.3 从jQuery 1.3开始

you no longer need the '@'. 你不再需要'@'。 Correct way to select is: 正确的选择方式是:

$("input[name='lom']")

If you have your radios in a container with id = radioButtonContainerId you can still use onClick and then check which one is selected and accordingly run some functions: 如果你的无线电放在一个id = radioButtonContainerId的容器中,你仍然可以使用onClick,然后检查选择了哪一个,并相应地运行一些功能:

$('#radioButtonContainerId input:radio').click(function() {
    if ($(this).val() === '1') {
      myFunction();
    } else if ($(this).val() === '2') {
      myOtherFunction();
    } 
  });
<input type="radio" name="radio" value="creditcard" />
<input type="radio" name="radio" value="cash"/>
<input type="radio" name="radio" value="cheque"/>
<input type="radio" name="radio" value="instore"/>

$("input[name='radio']:checked").val()

this should be good 这应该是好的

$(document).ready(function() {
    $('input:radio').change(function() {
       alert('ole');
    });
});

There are several ways to do this. 有几种方法可以做到这一点。 Having a container around the radio buttons is highly recommended regardless, but you can also put a class directly on the buttons. 无论如何,强烈建议在单选按钮周围放置一个容器,但您也可以直接在按钮上放置一个类。 With this HTML: 使用此HTML:

<ul id="shapeList" class="radioList">
<li><label>Shape:</label></li>
<li><input id="shapeList_0" class="shapeButton" type="radio" value="Circular" name="shapeList" /><label for="shapeList_0">Circular</label></li>
<li><input id="shapeList_1" class="shapeButton" type="radio" value="Rectangular" name="shapeList" /><label for="shapeList_1">Rectangular</label></li>
</ul>

you can select by class: 你可以按班选择:

$(".shapeButton").click(SetShape);

or select by container ID: 或按容器ID选择:

$("#shapeList").click(SetShape);

In either case, the event will trigger on clicking either the radio button or the label for it, though oddly in the latter case (Selecting by "#shapeList"), clicking on the label will trigger the click function twice for some reason, at least in FireFox; 在任何一种情况下,事件都将在单击单选按钮或其标签时触发,但奇怪的是在后一种情况下(通过“#shapeList”选择),单击标签将因某种原因触发单击功能两次,至少在FireFox中; selecting by class won't do that. 按类选择不会那样做。

SetShape is a function, and looks like this: SetShape是一个函数,看起来像这样:

function SetShape() {
    var Shape = $('.shapeButton:checked').val();
//dostuff
}

This way, you can have labels on your buttons, and can have multiple radio button lists on the same page that do different things. 这样,您可以在按钮上添加标签,并且可以在同一页面上有多个单选按钮列表,这些列表可以执行不同的操作。 You can even have each individual button in the same list do different things by setting up different behavior in SetShape() based on the button's value. 您甚至可以通过在SetShape()中根据按钮的值设置不同的行为,让同一列表中的每个按钮执行不同的操作。

it is always good to restrict the DOM search. 限制DOM搜索总是好的。 so better to use a parent also, so that the entire DOM won't be traversed. 所以最好也使用父级,这样就不会遍历整个DOM。

IT IS VERY FAST 这非常快

<div id="radioBtnDiv">
  <input name="myButton" type="radio" class="radioClass" value="manual" checked="checked"/>
 <input name="myButton" type="radio" class="radioClass" value="auto" checked="checked"/>
</div>



 $("input[name='myButton']",$('#radioBtnDiv')).change(
    function(e)
    {
        // your stuffs go here
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM