jQuery检查所有文本输入是否已禁用然后继续

时间:2013-09-17 09:21:54

标签: jquery

单击按钮时,表单将通过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);
            }
        });
    });

2 个答案:

答案 0 :(得分: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');
}

答案 1 :(得分:0)

.each是一个回调,所以它会立即返回,你应该改变你的每一个,以便它不会返回:

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

相关问题