JavaScript - 一种检查

发布于 2024-10-20 20:45:16 字数 1093 浏览 2 评论 0原文

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

 <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>

所以这样做:

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

但是这个 JavaScript 对于某些 select-s 有效,而对于其中一些则无效。显然,当选择“- Month -”时,它返回“- Month -”而不是“”(空字符串)。我做错了什么?检查标签选择的最佳方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我为君王 2024-10-27 20:45:16

浏览器并不总是有

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

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

当然,如果您知道第一个选项始终是一个空白选项,则只需检查 selectedIndex==0 即可。

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

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

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.

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

绅士风度i 2024-10-27 20:45:16

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

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

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!');
}
指尖上的星空 2024-10-27 20:45:16

您不需要使用“selectedIndex”

<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>
    
    if (document.getElementById('Birth_Month').value === '') 
    {
        alert('Please fill select Month!');
    }

<select name="Birth_Month">
    <option value="they_did_not_select_anything" 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>

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

You do not need to use 'selectedIndex'

<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>
    
    if (document.getElementById('Birth_Month').value === '') 
    {
        alert('Please fill select Month!');
    }

OR

<select name="Birth_Month">
    <option value="they_did_not_select_anything" 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>

if (document.getElementById('Birth_Month').value === 'they_did_not_select_anything') 
{
    alert('Please fill select Month!');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文