How to change a radio button value(如何更改单选按钮的值)
问题描述
可能重复:
如何使用 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.
这篇关于如何更改单选按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何更改单选按钮的值
基础教程推荐
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
