$.post work only with alert [closed]

2020-05-05 17:31发布

When i call function with alert is working parfectly :

function post() {
    var cvs = $('#client-nbr').val();
    var cs = $('#cs').val(); $.post('a.php' {postcvs:cvs,postch1:cs});
    alert("The function work");
}

And the html form like this :

<form action="b.php" method="post" onsubmit="return post()">

The problem is when i remove alert from function the $.post doesnt work :

function post() {
    var cvs = $('#client-nbr').val();
    var cs = $('#cs').val(); $.post('a.php' {postcvs:cvs,postch1:cs});
}

Pls help :)

1条回答
我欲成王,谁敢阻挡
2楼-- · 2020-05-05 17:49

Since post() doesn't have a return statement, the onsubmit function ends up returning undefined.

Since it doesn't return false, it doesn't cancel the normal behaviour of the form. Consequently, the form submits and unloads the page before the Ajax request resolves.

Putting in an alert delays the page unload until you click OK, which gives time for the Ajax request to resolve.

return false from the post function to cancel the normal submission of the form instead.

查看更多
登录 后发表回答