20
16

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.

jQueryでoptionタグにselectedを付与する際はattrではなくpropを使う

Posted at

概要

  • 会員登録フローで、あるプルダウンの項目が未入力の場合、デフォルトでvalue="4"の項目を選択した状態にしたい
  • attr()で実装すると、iPadで閲覧した際に、初期値の空白とvalue="4"の項目がselectedになってしまう
  • attr()の代わりに、prop()で実装することで、正常にvalue="4"の項目のみにselectedが付く

不具合が発生した例

if($('#view_method').val() == ""){
	$("#view_method option[value='4']").attr('selected', 'selected');
}

修正後


if($('#view_method').val() == ""){
	$("#view_method option[value='4']").prop('selected', true);
}

参考

20
16
1

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
20
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?