jQuery下拉选项:首先选择 [英] jQuery dropdown option:first selected
问题描述
我正在使用一些jQuery填充各种下拉列表.我能够使jquery显示当前值作为第一个选项.
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.
如下图所示,您将看到当前值为目标".但是在点击下拉按钮后,Target会列出两次:
As shown in the images below, you see the current value is Target. But after clicking the dropdown button, Target is listed twice:
这是当前jQuery的样子:
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.
要重申一下,如果当前值为Target,那么在单击下拉列表时,您应该仅将Target视为当前选定的项目,并且只能看到一次.
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.
推荐答案
如果要选择第一个选项,则代码如下:
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);
仅当$ eexist作为下拉列表中的值存在时,此选项才起作用
this should only work if $eexist exists as value in dropdown
由于您没有提供太多信息,所以很难说出来..至少在您提出问题时提供jsfiddle链接
Since you didn't provide much so it's hard to tell..At least provide a jsfiddle link when you ask a question
这篇关于jQuery下拉选项:首先选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!