javascript jquery单选按钮单击

javascript jquery单选按钮单击,javascript,jquery,Javascript,Jquery,我有两个单选按钮,jquery正在运行 <input type="radio" name="lom" value="1" checked> first <input type="radio" name="lom" value="2"> second 首先 第二 现在,通过一个按钮,我可以设置onClick来运行函数。当我点击其中一个单选按钮时,如何使其运行功能?您可以使用.change来实现所需功能 $("input[@name='lom']").change(fun

我有两个单选按钮,jquery正在运行

<input type="radio" name="lom" value="1" checked> first
<input type="radio" name="lom" value="2"> second
首先
第二

现在,通过一个按钮,我可以设置onClick来运行函数。当我点击其中一个单选按钮时,如何使其运行功能?

您可以使用
.change
来实现所需功能

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

您不再需要“@”。正确的选择方法是:

$("input[name='lom']")
这应该很好

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

如果您将收音机放在id=radioButtonContainerId的容器中,您仍然可以使用onClick,然后检查选择了哪一个,并相应地运行一些功能:

$('#radioButtonContainerId input:radio').click(function() {
    if ($(this).val() === '1') {
      myFunction();
    } else if ($(this).val() === '2') {
      myOtherFunction();
    } 
  });

限制DOM搜索总是好的。所以最好也使用父对象,这样就不会遍历整个DOM

速度非常快


$(“输入[name='myButton']”,$(“#radioBtnDiv”)。更改(
职能(e)
{
//你的东西放在这里
});

有几种方法可以做到这一点。强烈建议在单选按钮周围放置一个容器,但也可以直接在按钮上放置一个类。使用此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>
或按容器ID选择:

$("#shapeList").click(SetShape);
在任何一种情况下,事件都会在单击单选按钮或其标签时触发,但奇怪的是,在后一种情况下(通过“#shapeList”选择),出于某种原因,单击标签会触发两次单击功能,至少在FireFox中是这样;按类选择不会这样做

SetShape是一个函数,如下所示:

function SetShape() {
    var Shape = $('.shapeButton:checked').val();
//dostuff
}
这样,您可以在按钮上有标签,并且可以在同一页面上有多个单选按钮列表来执行不同的操作。您甚至可以根据按钮的值在SetShape()中设置不同的行为,让同一列表中的每个按钮执行不同的操作。


<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()
$(“输入[name='radio']:选中”).val()
谢谢。这对我来说很有效,因为我的字段是数组的一部分,我不能使用输入[name='xxx']符号-即使你按下tab键并用箭头键移动标记,这会有反应吗?这在mobile中不起作用,选择器在mobile中不起作用。请注意,从jQuery 1.3开始,你不再需要'@'。正确的选择方法是:
$(“input[name='lom'])
是否可以将此解决方案与下面@JohnIdol的解决方案集成到基于无线电值的条件语句中?ex
$(“input[name='lom']).change(function(){if($(this).val()=='1'){….//基于此处值的rest条件语句}
从jQuery 1.3开始,您不应该在name属性中使用“@”。如果您这样做,您将得到一个
jQuery.self-0a6570c…js?body=1:1464未捕获错误:语法错误,无法识别的表达式
错误。因此这不是选择的问题。注意,最好不要让表单元素响应单击,因为许多人使用ke导航表单yboard.虽然这个答案没有显示如何将代码连接到事件,但我喜欢它包含了如何检索单选按钮组值的信息。$($input[@name='lom'])。change(function(){//Do something here});后面缺少了一个。
function SetShape() {
    var Shape = $('.shapeButton:checked').val();
//dostuff
}
<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()