简体   繁体   English

如何检查HTML选择选项中是否包含任何值

[英]How to check if the HTML selection option contains any values in it

In Order to stop multiple requests to server incase if the HTML Selection option is not empty 如果HTML选择选项不为空,则为了阻止对服务器的多个请求

Could anybody please let me know how to check if the HTML Select is not empty 有人可以让我知道如何检查HTML Select是否为空

This is my HTML 这是我的HTML

<select name="" id="SCname">
     <option selected="true" style="display:none;">Select Screen</option>
</select>
var json = {
        "employees": [
            {
                "firstName": "John",
                "lastName": "Doe"
            },
            {
                "firstName": "Anna",
                "lastName": "Smith"
            },
            {
                "firstName": "Peter",
                "lastName": "Jones"
            }
        ]
};
var html = '';
for (var i = 0; i < json.employees.length; i++) {
    var firstName = json.employees[i].firstName;    
    var lastName = json.employees[i].lastName;
    html += '<option value="'+firstName+'">'+lastName+'</option>';
}
$('#SCname').append(html);
$('#SCname').show();

Could anybody please tell me how to check if this is empty or not ?? 有人可以告诉我如何检查它是否为空吗?

This is my jsfiddle . 这是我的jsfiddle

To determine the number of options in the SELECT , using javascript: 要确定SELECT的选项数量,请使用javascript:

if (document.getElementById('SCname').options.length == 1 ){  // 1 because it looks like you automatically added 1 via HTML
}

or jQuery: 或jQuery:

if ($('#SCname option').length == 1) {...}

It's not entirely clear if you asking how to determine the current selected option or the number of options... 您是否询问如何确定当前选择的选项或选项数量还不清楚...

Since you are using jQuery, use the .val() method : 由于您使用的是jQuery,请使用.val()方法

if ($("#SCname").val() !== "") { 
  // ... The selection is not empty.
}

Note: this requires that you're "empty" option has an empty value: value="" . 注意:这要求您的“空”选项具有一个空值: value=""

Example


If you also want to check if option elements exist in the select menu: 如果还要 检查 select菜单中是否存在option元素

var mySelect = $("#SCname");
if (mySelect.val()!=="" && mySelect.find("option").length>0) { 
  // ... The selection is _not_ empty.
}

You can use the JQuery size() method on the select element option which will return the number of elements (This number should be greater than 1 since you are holding a default element): 您可以在select element选项上使用JQuery size()方法,该方法将返回元素数(由于持有默认元素,因此该数应大于1 ):

if ($('#SCname option').size() > 1)
{
  // Do some stuff
}

Maybe I misuderstood your logic but, I would tell that since you are dynamically appending option elements, why not just check the lenght of employees object before doing any additional processing: 也许我误解了您的逻辑,但是,我想告诉您,由于您正在动态附加选项元素,为什么不在执行任何其他处理之前不只是检查employees对象的长度:

var html = '';
if (json.employees.length > 0) // Check your array size
{
  for (var i = 0; i < json.employees.length; i++) 
  { 
    var firstName = json.employees[i].firstName;
    var lastName = json.employees[i].lastName; html += ''+lastName+''; 
  } 
  $('#SCname').append(html); 
  $('#SCname').show();
}

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

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