选择和默认值以外的选项

时间:2012-11-27 18:23:16

标签: javascript

如果用户点击提交按钮从默认值以外的选择框中选择一个选项,使用javascript这里是我的代码,我将如何提醒用户

<select id = "Sel">
    <option value ="Song1">default value<br>
    <option value ="Song2">Break on through<br>
    <option value ="Song3">Time<br>
    <option value ="Song4">Money<br>
    <option value="Song5">Saucerful of Secrets
</select><br><br>

继承人我的javascript代码我不确定从哪里开始,但这不会运行

if(document.getElementByID("Sel").value = "default value")
{
    alert("Pick another option besides default value");
}

谢谢!

4 个答案:

答案 0 :(得分:0)

您的标记首先是错误的。需要关闭打开选项标记

在更改事件中检查值... 如果value is Song1,则提醒用户

<强> HTML

<select id = "Sel">
    <option value ="Song1">default value</option>
    <option value ="Song2">Break on through</option>
    <option value ="Song3">Time</option>
    <option value ="Song4">Money</option>
    <option value="Song5">Saucerful of Secrets</option>
</select>​

<强>的Javascript

var elem = document.getElementById('submit');

elem.addEventListener('click', function(){
   var selectElem = document.getElementById('Sel');
   var val = selectElem.value;
    if(val == 'Song1'){
        alert('Select an option!!');
        return false;
    }        
});​

<强> Check Fiddle

答案 1 :(得分:0)

试试这个:

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#Sel').change(function() {
                    if($(this).val()=="Song1"){
                        alert('bad');
                    }else{
                        alert('good');
                    }
                });
                $('#button-test').click(function() {
                    var bob = $('#Sel').val();
                    if (bob=='Song1'){
                        alert('bad');
                    }else{
                        alert('good');
                    }
                });
            });
        </script>
    </head>
<body>

<select id = "Sel">
    <option value ="Song1">default value</option>
    <option value ="Song2">Break on through</option>
    <option value ="Song3">Time</option>
    <option value ="Song4">Money</option>
    <option value="Song5">Saucerful of Secrets</option>
</select><br>
<br>
<button id="button-test">

答案 2 :(得分:0)

首先,您的标记不正确..将其更正为

<select id = "Sel">
    <option value ="Song1">default value</option>
    <option value ="Song2">Break on through</option>
    <option value ="Song3">Time</option>
    <option value ="Song4">Money</option>
    <option value="Song5">Saucerful of Secrets</option>
</select><br><br>

然后在表单提交检查

if(document.getElementById('Sel').value=="Song1")
    return false;

OR

if(document.getElementById('Sel').selectedIndex ==0)
    return false;

答案 3 :(得分:0)

<script>
     $(document).ready(function() {
        $("#Sel").live('click', function (event) {
        var myval=$(this).val;
        alert(myval);
        });
    </script>   
    <select id = "Sel">
            <option name="opSel" value ="Song1">default value</option>
            <option name="opSel" value ="Song2">Break on through</option>
            <option name="opSel" value ="Song3">Time</option>
            <option name="opSel" value ="Song4">Money</option>
            <option name="opSel" value="Song5">Saucerful of Secrets</option>
        </select>