我试图用jQuery来检查一个单选按钮。以下是我的代码。
<form>
<div id='type'>
<input type='radio' id='radio_1' name='type' value='1' />
<input type='radio' id='radio_2' name='type' value='2' />
<input type='radio' id='radio_3' name='type' value='3' />
</div>
</form>
和JavaScript。
jQuery("#radio_1").attr('checked', true);
不起作用。
jQuery("input[value='1']").attr('checked', true);
不起作用。
jQuery('input:radio[name="type"]').filter('[value="1"]').attr('checked', true);
不起作用。
你有别的想法吗?我错过了什么?
对于jQuery的版本等于或高于(>=)1.6,使用。
$("#radio_1").prop("checked", true);
对于(<)1.6之前的版本,使用。
$("#radio_1").attr('checked', 'checked');
提示:你可能还想在事后对单选按钮调用click()
或change()
。更多信息请看评论。
试试这个。
在这个例子中,我以它的输入名称和值为目标。
$("input[name=background][value='some value']").prop("checked",true);
很高兴知道。 在多字值的情况下,它也可以工作,因为省略号。
试试这个。
使用值来检查单选按钮,使用这个。
$('input[name=type][value=2]').attr('checked', true);
或
$('input[name=type][value=2]').attr('checked', 'checked');
或
$('input[name=type][value=2]').prop('checked', 'checked');
使用ID来检查单选按钮,请使用以下方法。
$('#radio_1').attr('checked','checked');
或
$('#radio_1').prop('checked','checked');
$.prop
的方式更好。
$(document).ready(function () {
$("#radio_1").prop('checked', true);
});
你可以像下面这样测试。
$(document).ready(function () {
$("#radio_1, #radio_2", "#radio_3").change(function () {
if ($("#radio_1").is(":checked")) {
$('#div1').show();
}
else if ($("#radio_2").is(":checked")) {
$('#div2').show();
}
else
$('#div3').show();
});
});
试试这个
$(document).ready(function(){
$("input[name='type']:radio").change(function(){
if($(this).val() == '1')
{
// do something
}
else if($(this).val() == '2')
{
// do something
}
else if($(this).val() == '3')
{
// do something
}
});
});
试试这个例子
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myForm">
<input type="radio" name="radio" value="first"/> 1 <br/>
<input type="radio" name="radio" value="second"/> 2 <br/>
</form>
<script>
$(document).ready(function () {
$('#myForm').on('click', function () {
var value = $("[name=radio]:checked").val();
alert(value);
})
});
</script>
获取价值。
$("[name='type'][checked]").attr("value");
设定值: {{{8904945}}
$(this).attr({"checked":true}).prop({"checked":true});
单选按钮点击添加attr选中。
$("[name='type']").click(function(){
$("[name='type']").removeAttr("checked");
$(this).attr({"checked":true}).prop({"checked":true});
});
我'对不起英语。
<!--开始片段。 js hide: false console: true babel.false --> -- begin snippet: js hide: false console: true false -->
var $j = jQuery.noConflict();
$j(function() {
// add handler
$j('#radio-1, #radio-2').click(function(){
// find all checked and cancel checked
$j('input:radio:checked').prop('checked', false);
// this radio add cheked
$j(this).prop('checked', true);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset class="section">
<legend>Radio buttons</legend>
<label>
<input type="radio" id="radio-1" checked>
Option one is this and that—be sure to include why it's great
</label>
<br>
<label>
<input type="radio" id="radio-2">
Option two can be something else
</label>
</fieldset>
<!--结束片段-->
function rbcitiSelction(e) {
debugger
$('#trpersonalemail').hide();
$('#trcitiemail').show();
}
function rbpersSelction(e) {
var personalEmail = $(e).val();
$('#trpersonalemail').show();
$('#trcitiemail').hide();
}
$(function() {
$("#citiEmail").prop("checked", true)
});
我有一些相关的例子需要加强,如果我想添加一个新的条件,比方说,如果我想在点击项目状态值后隐藏颜色方案,除了铺路和铺路板。
例子在这里。
$(function () {
$('#CostAnalysis input[type=radio]').click(function () {
var value = $(this).val();
if (value == "Supply & Lay") {
$('#ul-suplay').empty();
$('#ul-suplay').append('<fieldset data-role="controlgroup"> \