最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set a selected option using ajax? - Stack Overflow

programmeradmin1浏览0评论

I am new to jquery and ajax. I am trying to set a selected option in my dropdown using my ajax code below:

$.ajax({
        type: "POST",
        url: "sample.php",
        cache: "false",
        dataType: "json",
        success: function(data) {
          //data.month = 03
          $('#birth_month option[value="data.month"]').prop('selected', true);
        }
  });

This is my select html code:

<select id="birth_month" name="birth_month">
<option value="" disabled selected>Month</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>

And for some reason, it doesn't work. What am I doing wrong?

Thank you in advance for the suggestions.

I am new to jquery and ajax. I am trying to set a selected option in my dropdown using my ajax code below:

$.ajax({
        type: "POST",
        url: "sample.php",
        cache: "false",
        dataType: "json",
        success: function(data) {
          //data.month = 03
          $('#birth_month option[value="data.month"]').prop('selected', true);
        }
  });

This is my select html code:

<select id="birth_month" name="birth_month">
<option value="" disabled selected>Month</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>

And for some reason, it doesn't work. What am I doing wrong?

Thank you in advance for the suggestions.

Share Improve this question edited Nov 7, 2016 at 17:48 newbie asked Nov 7, 2016 at 17:44 newbienewbie 1451 gold badge4 silver badges8 bronze badges 1
  • You are giving data.month as string. Use + to append the value instead. Eg $('#birth_month option[value="+data.month+"]').prop('selected', true); – Lucky Commented Nov 7, 2016 at 17:46
Add a comment  | 

4 Answers 4

Reset to default 9

You have forgotten to concatenate the real value:

$('#birth_month option[value="'+data.month+'"]').prop('selected', true);

This should to the trick but you can use an easier instruction:

$("#birth_month").val(data.month)

As date_month is a variable you need to create valid selector using string concatenation

Use .val(), to set value

$('#birth_month').val(data.month);

You can also use it:

$('#birth_month option[value="' + data.month + '"]').prop('selected', true);

Try like this,

 $("#birth_month").val(data.month).attr('selected','selected');
发布评论

评论列表(0)

  1. 暂无评论