问题描述
可能重复:
如何使用 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.
这篇关于如何更改单选按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!





大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)