2011-03-24 65 views
0

我有一個「保存設置」按鈕,其類型爲'圖像'。 我想一個複選框被用戶 我米使用檢查後,才啓用..使用jQuery啓用基於條件的按鈕

$('#btnSaveProfile').attr("disabled",true);  
$('#btnSaveProfile').click(function(){ 
      if ($("#rdAccept").is(':checked')) 
      { 
       $('#btnSaveProfile').attr("disabled",false); 
       updateProfile();// calling a function here that saves data. 
      } 
     }); 

這是不行的,任何輸入....請

回答

1

您沒有使用複選框單擊事件的按鈕,請嘗試以下

var $btn = $('#btnSaveProfile').attr("disabled",true); 

$('#rdAccept').click(function(){ 
    if (this.checked) { 
     $btn.removeAttr("disabled"); 
    } 
}); 

$btn.click(updateProfile); 
+0

但是,當複選框被選中時,這將調用'updateProfile()',而不是單擊(啓用)按鈕時。 – jensgram 2011-03-24 10:02:25

+0

@jensgram - 同意雖然這不是他的主要問題在這裏!更改 – redsquare 2011-03-24 10:03:08

+0

不,但我想這是OP的意圖:)好吧,編輯似乎很完美。 – jensgram 2011-03-24 10:05:10

1

$('#btnSaveProfile').attr("disabled",''); 使按鈕

$('#btnSaveProfile').attr("disabled",'disabled'); 禁用

2

您正在禁用ID爲btnSaveProfile的元素,然後附加一個點擊處理程序,該程序將永遠不會運行,因爲它被禁用。您需要將點擊處理程序添加到您的複選框,這將重新啓用保存按鈕。

+1

然後仍然將'updateProfile'保存爲'#btnSaveProfile'的'.click()'處理程序。例如,'$('#btnSaveProfile')。click(updateProfile);',[see here](http://jsfiddle.net/jensgram/8y3qM/3/)。 – jensgram 2011-03-24 09:56:38