Jquery Radio Button uncheck radio button, but should not clear value part

守給你的承諾、 提交于 2019-12-24 03:44:06

问题


I am trying to clear my form fields on reset button using following code

$(':input',formId).not(':button, :submit, :reset, :hidden').val('').prop('checked', false).removeAttr('selected');

Now my Radio button is getting unchecked, but value set in radio button is also getting cleared.

I want only to uncheck it, But value of the radio button should be available.


回答1:


You can use reset() function here:

formId.reset();

reset resets the form to its initial state.

FIDDLE DEMO




回答2:


don't clear the value, i.e. remove .val(''). This is setting your value to blank. so you want to do this

$(':input[type!=checkbox]',formId).not(':button, :submit, :reset, :hidden').val('')
        .removeAttr('selected');

then this

$(':input[type=checkbox]',formId).not(':button, :submit, :reset, :hidden')
        .removeAttr('checked');

you don't need .removeAttr('selected'). Chekboxes only have a checked attribute, see w3c here

more info on the selectors used here



来源:https://stackoverflow.com/questions/17234270/jquery-radio-button-uncheck-radio-button-but-should-not-clear-value-part

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!