How to set the 'selected option' of a select dropdown list with jquery

后端 未结 6 1490
南笙
南笙 2021-01-31 09:07

I have the following jquery function:

$.post(\'GetSalesRepfromCustomer\', {
    data: selectedObj.value
}, function (result) {
    alert(result[0]);
    $(\'sele         


        
相关标签:
6条回答
  • 2021-01-31 09:25

    The match between .val('Bruce jones') and value="Bruce Jones" is case-sensitive. It looks like you're capitalizing Jones in one but not the other. Either track down where the difference comes from, use id's instead of the name, or call .toLowerCase() on both.

    0 讨论(0)
  • 2021-01-31 09:32

    Try this :

    $('select[name^="salesrep"] option[value="Bruce Jones"]').attr("selected","selected");
    

    Just replace option[value="Bruce Jones"] by option[value=result[0]]

    And before selecting a new option, you might want to "unselect" the previous :

    $('select[name^="salesrep"] option:selected').attr("selected",null);
    

    You may want to read this too : jQuery get specific option tag text

    Edit: Using jQuery Mobile, this link may provide a good solution : jquery mobile - set select/option values

    0 讨论(0)
  • 2021-01-31 09:32

    Set the value it will set it as selected option for dropdown:

    $("#salesrep").val("Bruce Jones");
    

    Here is working Demo

    If it still not working:

    1. Please check JavaScript errors on console.
    2. Make sure you included jquery files
    3. your network is not blocking jquery file if using externally.
    4. Check your view source some time exact copy of element stop jquery to work correctly
    0 讨论(0)
  • 2021-01-31 09:48

    You have to replace YourID and value="3" for your current ones.

    $(document).ready(function() {
      $('#YourID option[value="3"]').attr("selected", "selected");
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
    <select id="YourID">
      <option value="1">A</option>
      <option value="2">B</option>
      <option value="3">C</option>
      <option value="4">D</option>
    </select>

    and value="3" for your current ones.

    $('#YourID option[value="3"]').attr("selected", "selected");
    
    <select id="YourID" >
    <option value="1">A </option>
    <option value="2">B</option>
    <option value="3">C</option>
    <option value="4">D</option>
    </select>
    
    0 讨论(0)
  • 2021-01-31 09:50

    One thing I don't think anyone has mentioned, and a stupid mistake I've made in the past (especially when dynamically populating selects). jQuery's .val() won't work for a select input if there isn't an option with a value that matches the value supplied.

    Here's a fiddle explaining -> http://jsfiddle.net/go164zmt/

    <select id="example">
        <option value="0">Test0</option>
        <option value="1">Test1</option>
    </select>
    
    $("#example").val("0");
    alert($("#example").val());
    $("#example").val("1");
    alert($("#example").val());
    
    //doesn't exist
    $("#example").val("2");
    //and thus returns null
    alert($("#example").val());
    
    0 讨论(0)
  • 2021-01-31 09:50

    $(document).ready(function() {
      $('#YourID option[value="3"]').attr("selected", "selected");
      $('#YourID option:selected').attr("selected",null);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
    <select id="YourID">
      <option value="1">A</option>
      <option value="2">B</option>
      <option value="3">C</option>
      <option value="4">D</option>
    </select>

    0 讨论(0)
提交回复
热议问题