javascript 如何更改单选按钮值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11700252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:02:48  来源:igfitidea点击:

How to change a radio button value

javascriptjqueryhtmlradio-button

提问by undefined

Possible Duplicate:
how to check a radio button with jQuery?

可能的重复:
如何使用 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="checked" />Search Vehicle
        &nbsp;&nbsp; <input type="radio" name="search" id="search-damages"
            value="search-damages" /> 

回答by undefined

How to change a radio button value?

如何更改单选按钮值?

checkedproperty doesn't change the value of radio button. note that checkedproperty 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.

选中的属性不会更改单选按钮的值。请注意,checked属性是 DOM INPUT 元素属性之一,而不是 jQuery 对象属性之一(通过使用 jQuery 选择一个元素,将创建一个 jQuery 对象)。如果您想更改此属性,您应该首先将 jQuery 对象转换为 DOM 元素对象。

$('#search-damages')[0].checked = true;

or use jQuery object's prop()method:

或使用 jQuery 对象的prop()方法:

$('#search-damages').prop('checked', true)

If you want to change the value(not checked property) of a Radio Button Control you can use jQuery val()method:

如果要更改单选按钮控件的值(未选中的属性),可以使用 jQueryval()方法:

$('#search-damages').val('new value');

please note that :radioselector is deprecated and as your checkbox has an ID, you can select it by ID selector.

请注意:radio选择器已被弃用,因为您的复选框有一个 ID,您可以通过 ID 选择器选择它。

回答by M. Ahmad Zafar

Try this:

试试这个:

$('#search-damages').attr('checked','checked');

回答by ZER0

I'm a fan of "use jQuery when reduce complexity, use JS in other cases", so I would go for:

我是“在降低复杂性时使用 jQuery,在其他情况下使用 JS”的粉丝,所以我会去:

$("#search-damages")[0].checked = true;

Otherwise, if you prefer use jQuery syntanx, you could use:

否则,如果您更喜欢使用 jQuery 语法,您可以使用:

$("#search-damages").prop("checked", true);

See: http://api.jquery.com/prop/

见:http: //api.jquery.com/prop/