问题描述
可能重复:
如何使用 jQuery 检查单选按钮?
如何更改单选按钮的值.
How to change a radio button value.
jQuery('input:radio[name=search][id=search-damages]').checked = true;
我已经试过了,但它不起作用
i have tried this but it is not working
<input type="radio" name="search" id="search-vehicle" value="search-vehicle" checked>
<input type="radio" name="search" id="search-damages" value="search-damages">
推荐答案
如何更改单选按钮的值?
How to change a radio button value?
checked 属性不会改变单选按钮的值.请注意,checked 属性是 DOM INPUT 元素属性之一,而不是 jQuery 对象属性之一(通过使用 jQuery 选择元素,会创建一个 jQuery 对象).如果您想更改此属性,您应该首先将 jQuery 对象转换为 DOM Element 对象.
checked property doesn't change the value of radio button. note that checked property is one the DOM INPUT Element properties not one of the jQuery object properties(by selecting an element using jQuery, a jQuery object is created). If you want to change this property you should first convert the jQuery object to a DOM Element object.
$('#search-damages')[0].checked = true;
或者使用jQuery对象的prop()
方法:
or use jQuery object's prop()
method:
$('#search-damages').prop('checked', true)
如果你想改变单选按钮控件的值(未选中属性),你可以使用 jQuery val()
方法:
If you want to change the value(not checked property) of a Radio Button Control you can use jQuery val()
method:
$('#search-damages').val('new value');
请注意 :radio
选择器已弃用,由于您的复选框有 ID,您可以通过 ID 选择器选择它.
please note that :radio
selector is deprecated and as your checkbox has an ID, you can select it by ID selector.
这篇关于如何更改单选按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!