问题
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