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.
这篇关于如何更改单选按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何更改单选按钮的值


基础教程推荐
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01