Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
7
Help us understand the problem. What is going on with this article?
@shibe23

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

More than 1 year has passed since last update.

概要

  • 会員登録フローで、あるプルダウンの項目が未入力の場合、デフォルトで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);
}

参考

7
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
shibe23

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
7
Help us understand the problem. What is going on with this article?