changing attribute state with Jquery

This question already has an answer here:

  • .prop() vs .attr() 17 answers

  • The attr method changes an element attribute, which is the initial state. The prop value changes the element property, which is the current state.

    To set the checked state, use the prop method:

    $('input[name=giustificaEntrata]').prop('checked', true);'
    

    To select an option, use the val method:

    $('select[name=pippo]').val('beta');
    

    The checkbox checked property basically has two states. checked - true & checked - false.

    From prop documentation, According to the W3C forms specification, the checked attribute is a boolean attribute.

    Also, The checked attribute value does not change with the state of the checkbox, while the checked property does.

    $('[name="giustificaEntrata"]').prop('checked',true);
    
    链接地址: http://www.djcxy.com/p/27548.html

    上一篇: 通过数据属性选择元素

    下一篇: 用Jquery改变属性状态