[英]Jquery script:If Checkbox is checked then change the label text
我想顯示當前用戶名並啟用一個按鈕,如果該復選框已選中,但此腳本無法正常工作。看起來好像什么都沒有發生,即:if語句未觸發
HTML
<script>
<script>
if($('#chbPurchaseAgree').attr('checked')) {
$('#lblPurchaseAgreedByValue').val($('#hdnCompUserName').val());
$('#btnPurchaseContinue').attr("Enable", true);
}
</script>
<asp:HiddenField ID="" runat="server" ClientIDMode="Static"/> //hidden field is set in code behind
<div class="control-group" style="float: left;">
<asp:CheckBox ID="chbPurchaseAgree" runat="server" ClientIDMode="Static" CssClass="chkbox" Text="I have read this document and agreed to the terms and conditions"/>
</div>
<asp:Label ID="lblPurchaseAgreedByValue" ClientIDMode="Static" runat="server"></asp:Label>
我相信這是行不通的,因為在執行時沒有復選框。 不確定'asp'標記是什么,但是我想這是客戶端渲染的某種占位符。
因此,建議在DOM准備就緒時使用一些框架/語言回調來執行JavaScript。
您可能要嘗試使用jQuery的“ .val()”而不是“ .value”。 這條路:
<script>
if($('#chbPurchaseAgree').is(':checked')) {
$('#lblPurchaseAgreedByValue').val($('#hdnCompUserName').val());
}
</script>
有條件地檢查一下。 if($('#chbPurchaseAgree')。attr('checked'))
我希望這可以幫助您。
https://jsfiddle.net/ry4t9cdg/
<input id="chkbox" type="checkbox">
<input id="uname" type="text">
<button id="button" disabled>BUTTON</button>
$('#chkbox').on('change',function(){
var self = this;
$('#button').prop('disabled',!self.checked);
if(this.checked)
$('#uname').val('username');
else
$('#uname').val('');
})
好的,這是我遇到的解決方案
<script>
function checkedchanged() {
var chkbox = document.getElementById('chbPurchaseAgree');
if (chkbox.checked)
{
$('#lblPurchaseAgreedByValue').html($('#hdnCompUserName').val());
$('#lblPurchaseAgreedDateValue').html($('#hdnCompDate').val());
$('#btnPurchaseContinue').attr('disabled', false);
}
else
{
$('#lblPurchaseAgreedByValue').html("");
$('#lblPurchaseAgreedDateValue').html("");
$('#btnPurchaseContinue').attr('disabled', true);
}
</script>
<asp:Button ID="btnPurchaseContinue" ClientIDMode="Static" runat="server" Text="Continue" Enabled="false"
></asp:Button>
感謝sznowicki和santialurralde
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.