0

Hey all i have the following js code to validate a textbox:

function submitContactUs() {
   var canSubmit = true;

   if (($('#cf_name_txt').val() == "") || ($('#cf_name_txt').val().substr(0, 1) == "*") || ($('#cf_name_txt').length == 1)) {
    if (!$('#cf_name_txt').next('img').length) { //image not there yet
        $('#cf_name_txt').after(theErrorIcon + " id=\"errIcon_cfname\" />");
        $('#errIcon_cfname').vibrate(conf);
        canSubmit = false;
        console.log('false name');
    }
   } else {
    $('#errIcon_cfname').remove();
        console.log('true name');
   }

   if (canSubmit) {
        alert(canSubmit);
        //$("#sendDataContactus").click();
   }

When i first click the button to fire off that function i get the correct false but once i hit the button again (without changing anything) it for some reason comes back as being true??? Does anyone see anything wrong with the js to cause it to do that?

The values i get from the first button press is:

false name

The value i get from the second button press is:

messagebox saying true
4

1 回答 1

3

当您第二次单击时,您已经有了img标签,因此您的变量不会重置canSubmit = false;

将变量分配移到创建错误代码块之外,如下所示:

 if (($('#cf_name_txt').val() == "") || ($('#cf_name_txt').val().substr(0, 1) == "*") || ($('#cf_name_txt').length == 1)) {

    canSubmit = false;
    ...
于 2012-10-08T16:00:43.197 回答