[英]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.