简体   繁体   English

如何通过使用OnChange Javascript HTML验证表单

[英]How to Validate form by using OnChange Javascript HTML

I am trying to do an extra check on a form using OnChange so an alert box appears if the text if equal to a specific value BEFORE the form is submitted. 我正在尝试使用OnChange对表单进行额外检查,因此如果文本在提交表单之前等于特定值,则会出现一个警告框。 If i remove the If value then the alert box WILL appear but its not coming up if the field is set to 'No Longer Required' 如果我删除了If值,则警告框将出现,但如果该字段设置为“不再需要”,则警告框不会出现

<script type="text/javascript">
/*<![CDATA[*/
function extracheck(s) {

if (s.value == "No Longer Required"){
alert("message here");
}
}

/*]]>*/
</script>

 <form id="form2" name="form2" method="post" action="returnprocess.asp" onSubmit="return     validateForm(this)">
  <select name="Reason1" class="textfieldmedium" id="Reason1" onchange="extracheck(this)">
                        <option selected="selected">Reason for Return</option>
                       <option>No Longer Required</option>
                        <option>Duplicated Order</option>
                        <option>Choice of Two</option>
                        <option>Incorrectly Supplied</option>
                        <option>Incorrect for Application</option>
                        <option>Incorrectly Packaged</option>
                        <option>Late Delivery</option>
                        <option>Incorrectly Picked</option>
                        <option>Never Ordered</option>
                        <option>Surcharge Unit</option>
                        <option>Damaged</option>
                        <option>Poor Fitment</option>
                          </select>
      </form>

Why isnt the alert box coming up when there is an IF statement ? 为什么在有IF语句时不弹出警报框?

You can do: 你可以做:

function extracheck(s) {
    var e = document.getElementById("Reason1");
    if (e.options[e.selectedIndex].value == "No Longer Required") {
        alert("message here");
    }
}

Demo: http://jsbin.com/lotebiho/1/ 演示: http//jsbin.com/lotebiho/1/

Alert is coming because function is executing every time and not on selection 警报到来是因为函数每次都在执行,而不是在选择时执行

try this, 尝试这个,

<script>
    $(document).ready(function() {
        $("#Reason1").select(function() {
            if ($(".textfieldmedium").val() == "No Longer Required") {
                alert("Message here");
            }
        )};
    )};
</script>

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

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