检查下拉列表的选定选项是否不是第一个使用 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2675694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:25:02  来源:igfitidea点击:

Check if dropdown's selected option is not the first with JavaScript

javascripthtmlhtml-select

提问by Mahmoud

Below are the options that i have in my HTML code:

以下是我在 HTML 代码中的选项:

    <label id="subn">
      <select name="subs" id="subs">
        <option value="nothing">Choose a Subject</option>
        <option value="General Question">General Question</option>
        <option value="MemberShip Area">MemberShip Area</option>
        <option value="Others">Others</option>
      </select>
    </label>

I want to create JavaScript code that will check if the user selected an option other than the first.

我想创建 JavaScript 代码来检查用户是否选择了第一个以外的选项。

Here is what I tried:

这是我尝试过的:

if (document.getElementsByTagName('option') == "nothing"){
    document.getElementById("subn").innerHTML = "Subject is Required!";
    document.getElementById("subs").focus();
    return false;
}

回答by Nick Craver

You can check like this if nothingis going to be first (usually the case in my experience):

您可以像这样检查是否nothing要首先(根据我的经验通常是这种情况):

if (document.getElementById('subs').selectedIndex == 0){

To still compare based on the value, do this:

要仍然根据值进行比较,请执行以下操作:

var sel = document.getElementById('subs');
if (sel.options[sel.selectedIndex].value == 'nothing') {

You may wand to change your markup so the label is beside, like this:

你可能想改变你的标记,这样标签就在旁边,像这样:

<select name="subs" id="subs"></select><label id="subn" for="subs"></label>

Otherwise this part: .innerHTML = "Subject is Required!";will erase your <select>:)

否则这部分:.innerHTML = "Subject is Required!";将删除您的<select>:)

回答by Sarfraz

This should do it:

这应该这样做:

var index = document.your_form_name.subs.selectedIndex;
var value = document.your_form_name.subs.options[index].value;

if (value === "nothing"){
   // your further code here.........
}

回答by RoToRa

document.getElementsByTagName('option')gives a collection of all optionelements in the document and "nothing"is a string. Comparing a collection to a string is quite useless.

document.getElementsByTagName('option')给出option文档中所有元素的集合,"nothing"是一个字符串。将集合与字符串进行比较是非常无用的。

Also setting document.getElementById("subn").innerHTML = "Subject is Required!";will delete the selectelement, so document.getElementById("subs")wouldn't find anything any more.

设置document.getElementById("subn").innerHTML = "Subject is Required!";也会删除select元素,所以document.getElementById("subs")不会再找到任何东西。

If you just need to know if anything is selected check the selectedIndexproperty of the selectelement:

如果您只需要知道是否选择了任何内容,请检查元素的selectedIndex属性select

if (document.getElementById("subs").selectedIndex <= 0) {
  // nothing is selected
}

EDIT:Changed > 0to <= 0. I would assume that it should be checked if the user didn't select anything, too.

编辑:更改> 0<= 0. 我认为如果用户没有选择任何内容,也应该检查它。