简体   繁体   English

JavaScript下拉验证和警报

[英]Javascript dropdown validation and alert

I have tried to get it to work but I simply can't find the mistake. 我试图使其正常工作,但我根本找不到错误。 Also I have no idea how to make the selection which you have not selected to turn red or some color to alert you. 另外,我不知道如何将尚未选择的选择变成红色或某种颜色来提醒您。

<!DOCTYPE html>
<html>
    <body>
        <select id='Selection' name='Selection'>
            <option value=''>Select</option>
            <option value='1'>user1</option>
            <option value='2'>user2</option>
            <option value='3'>user3</option>
        </select>
        <br>
        <select id='Candidate' name='Candidate'>
            <option value=''>Select</option>
            <option value='1'>candidate1</option>
            <option value='2'>candidate2</option>
            <option value='3'>candidate3</option>
        </select>  
        <br>  
        <input type='button' onclick='Validate()' value='select' />
        <script>
            function Validate()
            {
              if(document.getElementById('Selection').value == '' ||
                 document.getElementById('Candidate').value == '' ||)                
              {
                alert('Please complete all selections');
                return false;
              }
              return true;
            }       
        </script>
    </body>
</html>

You have an extra || 你有多余的|| in you if condition, this will cause a syntax error, remove it ( the last one not both ). 在您遇到的情况下,这将导致语法错误,请将其删除( 最后一个不是全部都删除)。 To change the element that has an invalid value you can just use css but you'll have to check them individually. 要更改具有无效值的元素,您可以使用css,但必须单独检查它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM