检查是否未从下拉列表中选择项目

时间:2015-01-22 02:31:38

标签: javascript

我有一个使用PHP动态生成的下拉列表。它呈现的HTML类似于下面的标记 -

<select size="8" id="email" name="email">
    <option value="6" data-userId="uid6">kamala@gmail.com</option>
    <option value="8" data-userId="uid8">tk@g.com</option>
    <option value="3" data-userId="uid3">myexample.com</option>
    <option value="7" data-userId="uid7">samadhi@gmail.com</option>
    <option value="2" data-userId="uid2">facebook.com</option>
    <option value="4" data-userId="uid4">lankainstitute.com</option>
</select>

使用此标记,我想检查是否使用JavaScript选择了项目。如果没有选择项目,那么我需要收到一条警告消息。

我试过这样的事情。但我无法让它发挥作用。

JavaScript -

var email = document.getElementById("email");
var selectedValue = email.options[email.selectedIndex].value;

if (selectedValue == '') {
    alert("Please select a email");
}

希望有人可以帮助我。 谢谢。

1 个答案:

答案 0 :(得分:2)

您应该监听select元素的change事件,然后将所选选项的值解析为整数(parseInt(email.options[email.selectedIndex].value, 10)):

Example Here

var email = document.getElementById("email");

email.addEventListener('change', function (e) {
    var selectedValue = parseInt(email.options[email.selectedIndex].value, 10);

    if (selectedValue === 7) {
        alert("Please select a email");
    }
});

var email = document.getElementById("email");

email.addEventListener('change', function (e) {
    var selectedValue = parseInt(email.options[email.selectedIndex].value, 10);

    if (selectedValue === 7) {
        alert("Please select a email");
    }
});
<select size="8" id="email" name="email">
    <option value="6" data-userId="uid6">kamala@gmail.com</option>
    <option value="8" data-userId="uid8">tk@g.com</option>
    <option value="3" data-userId="uid3">myexample.com</option>
    <option value="7" data-userId="uid7">samadhi@gmail.com</option>
    <option value="2" data-userId="uid2">facebook.com</option>
    <option value="4" data-userId="uid4">lankainstitute.com</option>
</select>


...因为您正在验证是否选择了某个选项,所以您可以在这些方面使用更多内容:

Example Here

var email = document.getElementById("email"),
    validationButton = document.getElementById('validationButton');

validationButton.addEventListener('click', function (e) {
    var selectedValue = email.options[email.selectedIndex] ? email.options[email.selectedIndex].value : null;

    if (!selectedValue) {
        alert("Please select a email");
    }
});

var email = document.getElementById("email"),
    validationButton = document.getElementById('validationButton');

validationButton.addEventListener('click', function (e) {
    var selectedValue = email.options[email.selectedIndex] ? email.options[email.selectedIndex].value : null;

    if (!selectedValue) {
        alert("Please select a email");
    }
});
<select size="8" id="email" name="email">
    <option value="6" data-userId="uid6">kamala@gmail.com</option>
    <option value="8" data-userId="uid8">tk@g.com</option>
    <option value="3" data-userId="uid3">myexample.com</option>
    <option value="7" data-userId="uid7">samadhi@gmail.com</option>
    <option value="2" data-userId="uid2">facebook.com</option>
    <option value="4" data-userId="uid4">lankainstitute.com</option>
</select>

<button id="validationButton">Check if selected</button>