如何在selectbox上进行验证? [英] How do I put validation on selectbox?

查看:71
本文介绍了如何在selectbox上进行验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的选择框



This is my selectbox

<select id="DropDownSeverity" class="dropbtn"> <!-- severity and urgency hard coded for now -->
                		<option disabled="disabled" selected="selected">Severity ▼</option>
                    	<option value="Extreme">Extreme</option>
                		<option value="Minor">Minor</option>
                		<option value="Moderate">Moderate</option>
                		<option value="Severe">Severe</option>
                    	<option value="Unknown">Unknown</option>
                	</select>





这是我的按钮或提交按钮





This is my button or submit button

<td style="align:centre;height:30px; width:100px; background-color:  #148f77;"><input type="button" onclick="CreateAlert();" value="Create" id="CreatUpdateAlertButton"/></td>





我的尝试:





What I have tried:

<select required id="DropDownSeverity" class="dropbtn"> <!-- severity and urgency hard coded for now -->
                		<option disabled="disabled" selected="selected">Severity ▼</option>
                    	<option value="Extreme">Extreme</option>
                		<option value="Minor">Minor</option>
                		<option value="Moderate">Moderate</option>
                		<option value="Severe">Severe</option>
                    	<option value="Unknown">Unknown</option>
                	</select>





尝试了必需,js也显示警告,如果值为null但不起作用我。请帮助



tried required and js too to show alert if value is null but didn't worked for me. Please Help

推荐答案

在你的评论中,你要求一个没有JavaScript的解决方案,但这是不可能的。所以这是一个使用JavaScript的解决方案。

In your comment, you ask for a solution without JavaScript, but that's not possible. So here's a solution that does use JavaScript.


  1. 将第一个选项的value属性设置为 none


  1. Set the "value" attribute on your first "option" to "none":
<option disabled="disabled" selected="selected" value="none">Severity ▼</option>

  • 在验证码中,检查所选值是否不等于none

  • In your validation code, check that the selected value is not equal to "none":

    if (document.getElementById("DropDownSeverity").value === "none") {
        alert("Please select a severity.");
        return; // 'exit' the running function; remove this line if you don't want that
    }







  • 请注意,人们可以使用您的浏览器开发人员工具将元素的值更改为其他内容以绕过验证。 如果您将此值发送到服务器,请确保在服务器端验证它。如果您的应用程序纯粹是客户端,则无需担心。



    Note that people can use your browser developer tools to change the value of the element to something else to circumvent the validation. If you send this value to a server, make sure to validate it on the server-side as well. If your application is purely client-side, no need to worry about it.

    <带有Jquery的




    with Jquery:
    function CreateAlert(){
       if(


    (#DropDownSeverity选项:selected)。value()== null){
    //在此处提醒您的消息
    }
    }
    ("#DropDownSeverity option:selected").value()==null){ //alert your message here } }


    这篇关于如何在selectbox上进行验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆