简体   繁体   English

即使JavaScript验证返回false,JSP表单仍会提交

[英]JSP form still submitting even though JavaScript validation returning false

I have a JSP with the form "deleteCont" and a javascript file with the function "validateDelete". 我有一个形式为“ deleteCont”的JSP和一个具有功能为“ validateDelete”的javascript文件。 When I click the "Delete contact" button the "validateDelete" function is successfully called and the alert with the "alertmessage" is displayed. 当我单击“删除联系人”按钮时,将成功调用“ validateDelete”功能,并显示带有“ alertmessage”的警报。 However, the "ContactMaint" servlet is still called even though false is being returned. 但是,即使返回false,仍然会调用“ ContactMaint” servlet。 Please can someone tell me where I am going wrong? 请有人能告诉我我要去哪里错吗?

<form name="deleteCont"
    onsubmit="return validateDelete('${sessionScope.account}','${sessionScope.firstname}', '${sessionScope.lastname}')" 
    action="ContactMaint" method="post">
    <input type="submit" value="Delete contact" name="delContact">
</form>   



function validateDelete(account, contactFirstName, contactLastName) {
    urlstrg = "http://localhost:8080/SalesPoliticalMapping/JCheckContacts?account=" +
        account + "&firstname=" + contactFirstName + "&lastname=" + contactLastName;
    $.get(urlstrg, function (result, status) {
        if (result.length > 0) {
            var alertmessage = "Can't delect contact:";
            for (var i = 0; i < result.length; i++) {
                alertmessage += result[i];
            }
            alert(alertmessage);
            return false;
        }
    });
}

I have now amended my code as follows although I'm not entirely sure why: 现在,我不确定我为什么修改代码如下:

function validateDelete(account, contactFirstName, contactLastName) {
$.ajaxSetup({async: false});
urlstrg = "http://localhost:8080/SalesPoliticalMapping/JCheckContacts?account=" +  account + "&firstname=" + contactFirstName + "&lastname=" + contactLastName;

var valid = true;
var alertmessage = "";

$.get(urlstrg, function(result, status) {
   if (result.length > 0) {
   valid = false;    
   alertmessage = "Can't delect contact:";
   for (var i = 0; i < result.length; i++) {
       alertmessage += result[i];
        }
   }
 });

 if (valid===false) {
     alert(alertmessage);
 return false;}
 };

There are 2 problems: 有两个问题:

  1. You're firing an ajax request. 您正在发出ajax请求。 Ajax requests are by default asynchronous. 默认情况下,Ajax请求是异步的。 Immediately after the $.get() called, the code advances to the next line while the asynchronous request runs in the background in a completely different thread. 在调用$.get() ,代码立即前进到下一行,而异步请求在完全不同的线程中在后台运行。 As there's in your case apparently nothing after the $.get() , it immediately returns (implicitly as true ). 因为在您的情况下$.get()之后显然没有任何内容,它会立即返回(隐式为true )。

  2. You're returning from a callback function, not from the main function. 您是从回调函数返回的,而不是从main函数返回的。 The callback function only returns into the main function, not outside. 回调函数仅返回主函数,而不返回外部。 You need to return from the main function. 您需要从主函数返回。

One way is to always return false and explicitly submit the form on success. 一种方法是始终返回false并在成功时显式提交表单。

function validateDelete(...) {
    var $form = $(this);

    $.get(..., function(...) {

        if (success) {
            $form.submit();
        }
    });

    return false;
}

Another way is making the ajax request asynchronous by passing { async: false } option as 2nd argument of $.get() . 另一种方法是通过传递{ async: false }选项作为$.get()第二个参数来使ajax请求异步。

function validateDelete(...) {
    var valid = false;

    $.get(..., { async: true }, function(...) {

        if (success) {
            valid = true;
        }
    });

    return valid;
}

Please note that this problem has completely nothing to do with JSP. 请注意,此问题与JSP完全无关。 You'd still have had exactly the same problem when using the same script in another view technology, such as PHP, ASP, etc, and even in a plain HTML page. 在另一种视图技术(例如PHP,ASP等)甚至是纯HTML页面中使用相同的脚本时,您仍然会遇到完全相同的问题。 JSP is in the context of this question merely a HTML code generator and can better be omitted from the question in future problems with specifically HTML+CSS+JS/jQuery. 在这个问题的上下文中,JSP只是一个HTML代码生成器,在以后的问题中,特别是HTML + CSS + JS / jQuery,最好将其从该问题中省略。

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

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