简体   繁体   English

如果不存在具有该值的选项,则设置选择字段的所选值

[英]set selected value of select field if option with that value does not exist

I want to set the selected value of a select box if it exists, and to add it to the select box if it does not exist. 我要设置选择框的选择值(如果存在),如果不存在,则将其添加到选择框。

How do I do that? 我怎么做?

I have: 我有:

<select id="abc" name="abc">
</select>

I want: 我想要:

  $('#abc').val(123);

afterwards: 然后:

<select id="abc" name="abc">
<option value="123" selected>
</select>

You can append element 您可以附加元素

var html = '<option value="123">123</option>';
jQuery("#abc").append(html);

First thing is that you can use jQuery append method to add any html element to your page. 第一件事是您可以使用jQuery append方法向页面添加任何html元素。 In this case, you may need to append an option element. 在这种情况下,您可能需要附加一个option元素。 So first thing you need is to check if that option exists. 因此,您需要做的第一件事就是检查该选项是否存在。 If it does not you need to append it into the select element. 如果不是,则需要将其附加到select元素中。 In both cases( whether it exists or not), you can set the value as selected using val() method: 在这两种情况下(无论是否存在),都可以使用val()方法将值设置为所选值:

 $(document).ready(function(){ select123(); });//document ready function select123() { if( ! $("#abc option[value=123]").length ) { $("#abc").append('<option value="123">123</option>'); } $("#abc").val("123"); }//select123 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <select id="abc" name="abc"> </select> 

var valueExists = $("#abc option[value=123]");
if(!valueExists){

    ("#abc").append('<option>123</option>')
}



$("#abc option[value=123]")

will return empty array which is evaluted to "" so it is false otherwise Array of object of html Collection returned 将返回空数组,其值为""因此为false,否则返回html Collection的对象数组

How about the below code. 下面的代码怎么样。

if($('#abc option[value=' + testValue + ']').length === 0) {
    $('#abc').append('<option value="' + testValue + '">' + testValue + '</option>');
}

Check if the value exists using $("#abc option[value='123']").length > 0 and if not, append. 使用$("#abc option[value='123']").length > 0检查该值是否存在,如果不存在,则追加。

 $(function(){ var val = '1234'; if($("#abc option[value='123']").length > 0){ alert('present') } else { var s = '<option value=' + val + '>' + val + '</option>'; $('#abc').append(s); } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <select id="abc" name="abc"> <option value="123" selected> </select> 

Below code can help you started. 下面的代码可以帮助您开始。

  1. If the value is already present, then set it as the selected option. 如果该值已经存在,则将其设置为所选选项。
  2. Else append the value to dropdown. 否则将值附加到下拉列表中。

Scenario 1: Value already exists in dropdown. 方案1:下拉列表中已经存在值。 So just select it. 因此,只需选择它。

 $(function() { var val = '1'; var exists = false; $('#abc option').each(function() { if (this.value == val) { $('#abc').val(val); exists = true; return false; } }); if (exists == false) { $('#abc').append('<option value="' + val + '" selected>' + val + '</option>'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="abc" name="abc"> <option value="1">1</option> <option value="2">2</option> </select> 


Scenario 2: Value not in dropdown. 方案2:值不在下拉列表中。 So append it. 因此,附加它。

 $(function() { var val = '123'; var exists = false; $('#abc option').each(function() { if (this.value == val) { $('#abc').val(val); exists = true; return false; } }); if (exists == false) { $('#abc').append('<option value="' + val + '" selected>' + val + '</option>'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="abc" name="abc"> <option value="1">1</option> <option value="2">2</option> </select> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM