2011-09-03 51 views
4

手我有什么是我的小提琴这里http://jsfiddle.net/mPuj9/4/寻找我的jQuery和JS脚本

我试图做的是禁用按钮,直到所有的textbox ES被填充和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') 
     }); 
    }); 
}); 
+0

请不要发布仅包含链接到异地代码的问题。 “请修复这个代码块”的问题是“太本地化”的定义。 – meagar

回答

2

Here you go

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"); 
     } 
    }); 
}); 
+0

@Giorkouros - 看看这个小提琴。 – ShankarSangoli

+0

这很好,但一个小问题。当用户在文本框外单击时,提交按钮被禁用或启用。有什么办法可以在插入文本框的时候做到这一点?我在问,因为我使用jQuery datepicker作为一个文本框。 – EnexoOnoma

+0

这里你去http://jsfiddle.net/mPuj9/8/。还更新了答案。 – ShankarSangoli

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; 
     } 
    }); 
}); 
+0

这是你的代码http://jsfiddle.net/mPuj9/6/的小提琴,我仍然没有使它的工作。你能看看吗? – EnexoOnoma

+0

@Girokouros我现在就去做。 – kirb

+0

我发现了这个问题:我设置了按钮被禁用,而不是在所有内容都被填充时启用。[这里是jsfiddle工作版本的链接](http://jsfiddle.net/SkaEx/1/)。 – kirb

0

我认为问题在于你没有使用.each()作为文本字段。您正在检查最近一次更改的值是否为“”,但您并未检查每个值。