Get Text of checkbox with Jquery

后端 未结 5 611
故里飘歌
故里飘歌 2020-12-06 10:24
Pre-Payment

How would I get the text \"Pre-Payment\" from this using Jquery?

相关标签:
5条回答
  • 2020-12-06 10:44

    Check the below example and you get the script how to get label of checkbox in jQuery

    <html>
     <body>
         <ul>
             <li>
                 <input type="checkbox" name="mouse" value="1" id="mouse"  /><label for="mouse">Mouse</label>
                </li>
                <li>
                 <input type="checkbox" name="keyboard" value="1" id="keyboard"  /><label for="mouse">Keyboard</label>
                </li>
                <li>
                 <input type="checkbox" name="monitor" value="1" id="monitor"  /><label for="mouse">Monitor</label>
                </li>
            </ul>
        </body>
    </html>
    
    <script type="text/javascript">
     var label = jQuery('label[for=' + jQuery(this).attr('id') + ']').html();
     alert(label);
    </script> 
    

    Code taken from this link : http://chandreshrana.blogspot.in/2015/09/how-to-get-checkbox-label-text-jquery.html

    0 讨论(0)
  • 2020-12-06 10:55

    I'd recommend putting the text inside a <label> tag so that you could click on it (and so that screen readers and such could make sense of your form):

    <input type="checkbox" name="PrePayment" id="pre-payment">
    <label for="pre-payment">Pre-Payment</label>
    <br />
    

    Then, the whole thing becomes easy:

    var text    = $('label[for=pre-payment]').text();
    var or_this = $('#pre-payment').next('label').text();
    

    I'd prefer the first option, label[for=...], as it is less fragile than the second

    0 讨论(0)
  • 2020-12-06 10:58

    You should probably have a value attribute in the checkbox.

    <input type="checkbox" name="PrePayment" value="Pre-Payment">Pre-Payment<br />
    

    Then you can simply use the attr command:

    $(input).attr('value');
    
    0 讨论(0)
  • 2020-12-06 10:59

    I had a similar problem to this - another way you could make this work (with HTML5) is to use data attributes.

    As an example, you'd set a data attribute as follows:

    <input type="checkbox" name="PrePayment" data-text="Pre-Payment">Pre-Payment<br />
    

    You could then read this data attribute by using the following jQuery:

    $(input).data('text');
    

    The benefit of doing it this way is that you can have a seperate value parameter if required.

    0 讨论(0)
  • 2020-12-06 11:02

    Maybe:

    $("input[name='PrePayment']")[0].nextSibling.nodeValue;
    

    Try it here.

    0 讨论(0)
提交回复
热议问题