2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

(input[name=xxx]:checked)を使わないでradioボタンのcheckedを取る

Last updated at Posted at 2017-01-13

HTMLはこれ。

hoge.html
<form id="cancel-reason-form">
	<input type="radio" name="reason" value="1">
	<input type="radio" name="reason" value="2">
	<input type="radio" name="reason" value="3">
	<input type="radio" name="reason" value="4">
	<a id="cancel-reason-btn" href="#" class="btn-blue">送信する</a>
</form>

他の参考になる記事

【jQuery】ラジオボタンの状態取得と選択制御

こちらの記事で十分ですが、ページ内に、別のformと同じ名前のradioボタンがあった場合には困るので、このフォームの中のっていうのがやりたかったです。

[ちょいちょいわからなくなるcheckbox, radio, selectboxの値の取り方]
(http://qiita.com/nmta/items/dc29378adf79c5784218)

こちらの記事のように、formにidをつけてセレクタを使えば良いですが、IDつけられないこともあるかもなのでw

自分で書いたやつ

radioのnodelistをとってそのcheckedをfilterする

hoge.js
function sendCancelReason(){
	var $form = $('#cancel-reason-form');
	var reason = $($form[0].reason).filter(":checked").val();
	console.log(reason);
}

こんな風に取れます。

hoge.js
var reason = $($form[0].reason).filter(":checked").val();

hoge.js
var reason = $($form[0].reason).filter(":checked").first().val();

の方がいいんだろうか。。。

idのセレクタを使ってfilter

hoge.js
$("#cancel-reason-form input[name=reason]:checked").val()

めっちゃ短い…。これの方がいいか…。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?