简体   繁体   中英

Jquery script:If Checkbox is checked then change the label text

I want to show current user name and enable a button ,if the checkbox is checked , but this script not working..Looks like nothing is happening ie: if statement is not firing

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>

I believe it doesn't work because at the execution time there is no checkbox. Not sure what 'asp' tag is, but I guess this is some kind of placeholder for client side rendering.

So I'd suggest to use some framework/language callback to do the javascript execution when DOM is ready.

You might want to try with jQuery's ".val()" instead of ".value". This way:

<script>
if($('#chbPurchaseAgree').is(':checked')) {
   $('#lblPurchaseAgreedByValue').val($('#hdnCompUserName').val());
}
</script>

check this in the conditional. if($('#chbPurchaseAgree').attr('checked'))

i hope this can help you out.

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('');
})

Ok this is the solution I came across

 <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>

Thanks to sznowicki and santialurralde

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM