当submited与Ajax表单验证被忽略

当submited与Ajax表单验证被忽略

本文介绍了当submited与Ajax表单验证被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网页此JavaScript函数。

I have this javascript function in my page.

$(document).ready(function(){
    $('input[type="radio"]').click(function(){
        if($(this).attr("value")=="InfiStallStock"){
            $(".e-stock-saldo-box").hide();
            $(".InfiStall ul li .infistallLocation").attr('required');
            $(".Delivery ul input:radio").removeAttr('required');
            $(".Delivery ul input:text").removeAttr('required');
            $(".InfiStall").show();
        }
        if($(this).attr("value")=="DeliveryStock"){
            $(".e-stock-saldo-box").hide();
            $(".InfiStall ul li .infistallLocation").removeAttr('required');
            $(".Delivery ul li input:radio").attr('required');
            $(".Delivery ul input:text").attr('required');
            $(".Delivery").show();
        }
    });
});


            function enableList(element) {
                var select = document.getElementById("select"+element.id.substring(element.id.length-9));
                enableSubmit();
                showUp();
                if(element.checked === true){
                    select.disabled = false;
                    checkSelect(select);
                }else{
                    select.disabled = true;
                    select.selectedIndex = 0;
                }
            }
            function checkSelect(element){
                if(!validate_select(element)){
                    element.setCustomValidity("Pilih Jumlah Estock");
                }else{
                    element.setCustomValidity("");
                }
            }

这javascript的运作要检查的选项,通过用户选择。如果用户选择一个选项,然后它已要求输入另一选项将被忽略/删除,反之亦然。

This javascript functioned to check which option select by user. If user choose one option, then another option which has required input will be ignored/removed, and vice versa.

JavaScript的是伟大的工作在验证表单,如果我直接与表单操作提交表单如<形式的行动=submit.php/>

The javascript is working great in validate the form if I submit the form directly with form action like <form action="submit.php"/>.

不过,当我改变提交它现在是用Ajax的方式,验证被忽略。

However, when I change the way to submit it which is now with ajax, the validation is ignored.

下面是AJAX脚本来提交表单:

Here is the ajax script to submit the form:

$('#btn_submit').on('click', function(e) {
    e.preventDefault();

    var esquantity = $('.row_selected select').map(function() { return $(this).val(); }).get().join('|');
    var size = $('.row_selected td.size span').map(function() { return $(this).text(); }).get().join('|');

    $.ajax(
    {
        type: "POST",
        url: "../ajax.php",
        data: 'esquantity='+esquantity+ '&size='+ size,
        cache: false,
        success:function(html)
        {
            alert("Succes " + html);
        }
    });

任何人可以帮助我如何解决这个请,因此,这种形式将有一定的验证,即使我用Ajax提交。谢谢!

Can anybody help me how to solve this please, so that this form will have some validations even though I submit it with ajax. Thanks!

推荐答案

尝试 -

写这篇以下事件对正常的按钮,而不是提交按钮

Writing this below event on the normal button rather than on the submit button

$('#btn_submit').on('click', function(e) {

那么这将要求你不要叫

Which will then require you not to call the

e.preventDefault();

在功能,你可以做什么,然后是显式调用提交()函数在Ajax调用的回调面积即

in the function and what you can do then is to call the submit() function explicitly in the ajax call's callback area i.e.

success:function(html)
{
    // call submit function explicitly it should validate all your constraints then
}

这篇关于当submited与Ajax表单验证被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:33