如何在jQuery中预选单选按钮 [英] How to pre-select a radio button in jQuery

查看:106
本文介绍了如何在jQuery中预选单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想预选以下集合中的第一个单选按钮:

I want to pre-select the first radio button in the following set:

HTML

<input type="radio" value="0" name="serviceNoteType[1]">
<input type="radio" value="1" name="serviceNoteType[1]">

jQuery

var i = 1; // for sake of this example
$('[name="serviceNoteType\\['+ i +'\\]"]').prop('checked', true);

结果是第二个单选按钮被选中,而我希望第一个被选中.如何定位第一个单选按钮?

The result is that the second radio button gets selected and I want the first one selected. How to I target the first radio button?

推荐答案

您可以将.first()添加到选择器:

You can add .first() to your selector:

var i = 1; // for sake of this example
$('[name="serviceNoteType\\['+ i +'\\]"]').first().prop('checked', true);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="radio" value="0" name="serviceNoteType[1]">
<input type="radio" value="1" name="serviceNoteType[1]">

这篇关于如何在jQuery中预选单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆