JavaScript - 检查是否选择了 <select> 标签的选项的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5224071/
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-10-25 16:26:16  来源:igfitidea点击:

JavaScript - a way check if an option of the <select> tag is selected

javascriptselecttags

提问by Narek

I have a select tag and I want to check if a moth is selected.

我有一个选择标签,我想检查是否选择了飞蛾。

 <select name="Birth_Month">
    <option value="" SELECTED>- Month -</option>
    <option value="January">January</option>
    <option value="Fabruary">Fabruary</option>
    <option value="March">March</option>
    <option value="April">April</option>
    <option value="May">May</option>
    <option value="June">June</option>
    <option value="July">July</option>
    <option value="August">August</option>
    <option value="September">September</option>
    <option value="October">October</option>
    <option value="November">November</option>
    <option value="December">December</option>
</select>

So do this:

所以这样做:

if (document.registration_form.Birth_Month.value === '') 
{
    alert('Please fill select Month!');
}

But this JavaScript for some select-s work and for some of them, does not. Obviously, when the "- Month -" is selected the it returnes "- Month -" insted of "" (empty string). What I have done wrong? What is the best way of checking the tag's selection?

但是这个 JavaScript 对某些 select-s 有效,而对其中一些则无效。Obviously, when the "- Month -" is selected the it returnes "- Month -" insted of "" (empty string). 我做错了什么?检查标签选择的最佳方法是什么?

回答by Webveloper

Browsers didn't always have a .value property for <select> - we used to have to get the value of the <option>:

浏览器并不总是有 <select> 的 .value 属性 - 我们过去必须获取 <option> 的值:

var birthMonth=document.registration_form.Birth_Month;
if(birthMonth.options[birthMonth.selectedIndex].value===''){

I use jQuery .val() now. I don't remember which browsers lack the select.value property, and maybe those browsers are so old that we don't need to worry about them anymore. But jQuery doesn't use select.value - it loops through each of the options to find the selected option's value.

我现在使用 jQuery .val()。我不记得哪些浏览器缺少 select.value 属性,也许这些浏览器太旧了,我们不再需要担心它们了。但是 jQuery 不使用 select.value - 它遍历每个选项以查找所选选项的值。

Of course, if you know you'll always have a single blank option as the first option, just check for selectedIndex==0.

当然,如果你知道你总是有一个空白选项作为第一个选项,只需检查 selectedIndex==0。

回答by hex494D49

I believe integers are a better practice for option values. Anyhow, the snippet below doesn't care if your default option value is an empty string or a zero value.

我相信整数是选项值的更好实践。无论如何,下面的代码片段并不关心您的默认选项值是空字符串还是零值。

var birthMonth = document.registration_form.Birth_Month;
if(birthMonth.options[birthMonth.selectedIndex].value === false){
    alert('Please fill select Month!');
}