1

我有这个代码。该脚本在 ajax 成功时返回 true,但它不会将此值返回给函数,因此无法提交表单。请问有人可以帮忙吗?

更新:我修改了脚本:

jQuery代码:

$(document).ready(function () {
    $('#addForm').live('submit', function (e) {
        e.preventDefault();
        $(".error").remove();
        var client_name = $("#client_name").val();
        var project_name = $('#project_name').val();
        var source_lang = $('#source_lang option:selected').length;
        var job_no = $('#job_no').val();
        var targ_lang = $('#targ_lang option:selected').length;
        var supplier_id = $('supplier_id option:selected').length;
        var start_date = $('#start_date').val();
        var deadline = $('#deadline').val();
        var targ_lang_val = $('#targ_lang option:selected').val();
        var source_lang_val = $('#source_lang option:selected').val();
        var haserror = false;
        $.ajax({
            type : "GET",
            dataType : 'json',
            async : false,
            url : "file.php?job=" + job_no + "&client_name=" + client_name + "&project_name=" + project_name + "&source_lang=" + source_lang + "&targ_lang=" + targ_lang + "&supplier_id=" + supplier_id + "&start_date=" + start_date + "&deadline=" + deadline + "&targ_lang_val=" + targ_lang_val + "&source_lang_val=" + source_lang_val,
            success : function (data) {
                $.each(data, function (index, element) {
                    if (index == 'job_no') {
                        if (element == true) {
                            $("#" + index).after('<br><span class="error" style="color: red;">This Job No already exists.</span><br>');
                            haserror = true;
                        } else
                            haserror = false;
                    }
                    if (element == true) {
                        $("#" + index).after('<br><span class="error" style="color: red;">This field is required.</span><br>');
                        haserror = true;
                    } else
                        haserror = false;
                });
                return !haserror;
            }
        });
    });
});

服务器端PHP 代码

$client_name = $_GET['client_name'];
$deadline    = $_GET['deadline'];
if ($client_name == '') {
    $hasError['client_name'] = true;
} else
    $hasError['client_name'] = false;
// .........................
if ($deadline == '') {
    $hasError['deadline'] = true;
} else
    $hasError['deadline'] = false;
print json_encode($hasError);
4

1 回答 1

0

替换这个

return !haserror;

if(!haserror) $('#addForm')[0].submit()

DOM直接调用元素的提交方法FORM不会触发submit事件

(这里以小提琴为例http://jsfiddle.net/kKXa7/

于 2013-07-16T11:28:15.113 回答