2013-03-22 96 views
0

我正在開發MVC應用程序。 我有複選框並提交按鈕。如何使用JQuery禁用按鈕?

我想在複選框的選中事件上啓用提交按鈕,在未選中的提交按鈕應該禁用。

如何做到這一點?

我有下面的代碼....

@model PaymentAdviceEntity.Account 
    @using PaymentAdviceEntity; 


    @{ 
     ViewBag.Title = "Create"; 
     PaymentAdvice oPA = new PaymentAdvice(); 
     oPA = (PaymentAdvice)ViewBag.PaymentAdviceObject; 

     <div> 
      <input type="checkbox" class="optionChk" value="1" /> Paid 
     </div> 

    <input type="submit" value="Create" /> 
    <input type="button" class="btn-primary" /> 
    } 

    <script type="text/javascript"> 
$(".optionChk").on("change", function() { 
    if ($(this).is(":checked")) 
    { 
     alert("1"); 

     $(".SubmitButton").attr("disabled", "disabled"); 
    } else 
    { 
     alert("2"); 

     $(".SubmitButton").attr("enable", "enable"); 
    }  
}); 
</script> 
+1

可能複製http://stackoverflow.com/questions/577548/how-can-i的 - 禁用jQuery中的對話框 - 從函數對話 – rhughes 2013-03-22 13:14:32

回答

0

試試這個:

$(".optionChk").on("change", function() { 
    if ($(this).is(":checked")) { 
     $("input[type=submit]").removeAttr("disabled"); 
    } else { 
     $("input[type=submit]").attr("disabled", "disabled"); 
    }  
}); 

JSFiddle

+0

不工作,我應該寫$(「input [name = submit]」)。removeAttr(「disabled」);它是什麼? – Nil 2013-03-22 13:31:29

+0

@Nil更新的答案(提交被禁用複選框選中) – Sergio 2013-03-22 13:42:44

+0

它不適合你,因爲你沒有提交的名稱,所以更改選擇器爲'type = submit' – Sergio 2013-03-22 13:44:45

0

試試這個:

$(function(){ 
    $('.optionChk').click(function(){ 
     $('input[type="submit"]').toggle('fast'); 
    }); 

}); 

和HTML:

<div> 
     <input type="checkbox" class="optionChk" value="1" checked="checked" /> Paid 
    </div> 

<input type="submit" value="Create" /> 

工作FIDDLE

+0

如何在開始時設置未選中? – Nil 2013-03-22 13:39:08

+0

@Nil只是從複選框中刪除選中的屬性 – 2013-03-22 13:46:14

2

您應該使用prop設置/獲取disabled屬性

$(".optionChk").on("change", function() { 
    $("input[type=submit]").prop("disabled",!this.checked); 
}); 

而且您的提交按鈕沒有類=「提交」,所以你需要使用的屬性選擇器或給它的類='提交'並使用$('.Submit')代替$('input[type=submit]')

FIDDLE

0

禁用提交按鈕:

$(".optionChk").on("change", function() { 

if ($(this).is(":checked")) 
{ 
$("input[type='submit']").attr('disabled',false); //button will be enabled 
} 
else 
{ 
$("input[type='submit']").attr('disabled',true); //button will be disabled 
} 
}) 

代碼啓用或禁用提交按鈕:

$("input[type='submit']").attr('disabled',true); //button will be disabled