寻找我的jQuery和JS脚本

时间:2011-09-03 23:49:01

标签: javascript jquery

我所拥有的是我的小提琴http://jsfiddle.net/mPuj9/4/

我尝试做的是禁用该按钮,直到所有textbox被填充并且checkbox被检查。我目前的代码不好,它让你在某些情况下提交,我不知道如何使用checkbox

CODE

$(document).ready(function() {
    $('.textfield').blur(function() {
        if ($.trim(this.value) == "") {
            $('#btnUpdate').attr("disabled", true);
        }
        else {
            $('#btnUpdate').removeAttr("disabled");
        }
    });
});

$(document).ready(function() {
    $('#termid').change(function() {
        var val = $(this).val();
        $('#reservationdetails').empty().addClass('loading').load('../kratisis/forms/' + val + '.php', function() {
            $('#reservationdetails').removeClass('loading')
        });
    });
});

3 个答案:

答案 0 :(得分:2)

你去吧

http://jsfiddle.net/mPuj9/8/

$(document).ready(function () {
    $('form').delegate('input:text, input:checkbox', 'blur keyup change', function () {
        if(($('form input:text').filter(function(){ return $.trim(this.value) == ''; }).length > 0)
           || ($('form input:checked').length == 0)){
          $('#btnUpdate').attr("disabled", true);
        }
      else {
            $('#btnUpdate').removeAttr("disabled");
      }
    });
});

答案 1 :(得分:1)

这是你可以做到这一点的另一种方式,如果有人按下文本框中的输入,或者浏览器自动填充输入,这也是有效的:

$(function() {
    $("form button").attr("disabled", true); //disable buttons at first
    var inputs = 0; //keep count of the inputs
    $("form input, form select, form textarea").each(function() {
        inputs++; //increment counter
        $(this).keypress(function() {
            if ($.trim($(this).val()) != "") { //if the value isnt empty
                inputs--; //decrement counter
                if (inputs == 0) { //if theyre all filled in
                    $("form button").attr("disabled", false);
                }
            }
        }).trigger("keypress"); //in case it was autofilled by the browser
    });
    $("form").submit(function(e) { //prevent the form being submitted by pressing enter
        if (inputs != 0) { //if they arent all filled in
            alert("Some fields aren't filled in. Please fix them and try again."); //tell the user
            e.preventDefault(); //cancel sending the form
            return false;
        }
    });
});

答案 2 :(得分:0)

我认为问题在于你没有对文本字段使用.each()。您正在检查最近更改过的那个,看它的值是否为"",但您没有检查每个。