开发者

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

开发者 https://www.devze.com 2023-02-15 14:00 出处:网络
I have a select tag and I want to check if a moth is selected. <select name=\"Birth_Month\"> <option value=\"\" SELECTED>- Month -</option>

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 w开发者_运维技巧ork 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?


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 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!');
}


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!');
}
0

精彩评论

暂无评论...
验证码 换一张
取 消