jquery radioボタン clear時にvalueまで消える

formのリセットボタンを作成したが、radioボタンの値がpostされないという修正依頼があった際の修正メモ
目次
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$(function () { $(".clear-button").on("click", function () { clearForm(this.form); }); function clearForm (form) { $(form) .find("input, select, textarea") .not(":button, :submit, :reset, :hidden") .val("") .prop("checked", false) .prop("selected", false) ; $(form).find(":radio").prop("checked", true); } }); |
changeイベントで、デバックしてみると、radio属性のvalueが消えてしまっていたので下記に修正
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
## .not(":button, :submit, :reset, :hidden")に, :radioを追加 ## 調べてみるとcheckboxも同様の事象だったので、ついでに修正 $(function () { $(".clear-button").on("click", function () { clearForm(this.form); }); function clearForm (form) { $(form) .find("input, select, textarea") .not(":button, :submit, :reset, :hidden, :checkbox, :radio") .val("") .prop("checked", false) .prop("selected", false) ; $(form).find(":radio").prop("checked", false); $(form).find(":checkbox").prop("checked", false); } }); |
-
前の記事
WindowsのDNSキャッシュをクリアする 2019.10.25
-
次の記事
windows10でTypeScriptをインストールして実行してみる 2019.10.26
コメントを書く