Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using some jQuery to populate various dropdowns. I was able to get the jquery to show current value as the first option.

However, I need it to not only show as the first option, but as the currently selected option so that the selection doesn't appear twice in the dropdown.

As shown in the images below, you see the current value is Target. But after clicking the dropdown button, Target is listed twice:

enter image description here

enter image description here

Here is what the current jQuery looks like:

 $(function()
 {
   $eexist = $(this).attr('data-exist');
   $('#eexist option:first').val($eexist).text($eexist);
 }

Which goes into this modal form dropdown select:

 <div id="editCustModal">
   <form id="editCustForm" name="editCustForm">
     <label for="eexist">Existing/Target</label>
     <select class="form-control" id="eexist" name="eexist">
       <option></option>  // keeping this or not does nothing
     </select>
   </form>
 </div>

The value and the text are the words Target and Existing.

To reiterate, if the current value is Target, then when you click on the dropdown, you should only see Target as the currently selected item AND only see it once.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
136 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you want to select first option then below is the code:

$('select option:first-child').attr("selected", "selected");

But if you want to select the current value then below is the code:

$('#eexist option:first').val($eexist);

this should only work if $eexist exists as value in dropdown

Since you didn't provide much so it's hard to tell..At least provide a jsfiddle link when you ask a question


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...