0

单击按钮时,会通过 ajax 发送表单,但我想首先检查所有文本输入是否已禁用。我尝试了以下方法,但是当一个元素被启用时,console.log 返回done,这不是应该发生的。

当每个input[type=text]都正确填写时,他们将获得属性disabled="disabled"

jQuery

$('.vacancy-submit').click(function(){
        var isValid = false;
        $("input[type=text]").each(function(){
            if ( $(this).is('[disabled="disabled"]') ) {
                isValid = true;
            } else {
                isValid = false;
            }
        });
        if (!isValid){
            alert("Please fill in all the required fields (highlighted in red)");
            console.log('derp');
        } else {
            console.log('done');
        }
        return isValid;

        var company = $('input[name=company-title]').val();
        var location = $('input[name=location-title]').val();
        var term = $('input[name=term]:checked').val();
        var closedate = $('input[name=vacancy-date]').val();
        $.ajax({
            type:"post",
            url:"/assets/inc/add-vacancy.php",
            data:"company="+company+"&location="+location+"&term="+term+"&closedate="+closedate,
            success:function(data){
                console.log(data);
            }
        });
    });
4

2 回答 2

3

要检查是否所有输入都被禁用,请使用disabled-selector

var isValid = $("input[type=text]:not(:disabled)").length == 0;
if (!isValid) {
    alert("Please fill in all the required fields (highlighted in red)");
    console.log('derp');
    return isValid;
} else {
    console.log('done');
}
于 2013-09-17T09:24:07.060 回答
0

.each 是一个回调,因此它会立即返回,您应该更改每个回调,使其不返回:

https://stackoverflow.com/a/6483430/1823684

于 2013-09-17T09:28:24.310 回答