checkbox如何判断是否选中

checkbox在项目中使用的比较多,好多时候需要判断,或者作为某些逻辑的依据。

总结一下,拿到checkbox状态的方法。

    <label for="checkbox">
      <input type="checkbox" name="checkbox" id="checkbox" />选中与否
    </label>

方法:

        console.log( $('#checkbox').get(0).checked );

        console.log( $('#checkbox').is(':checked') );

        console.log( $('#checkbox').prop('checked') );


        // 上面三种方法都可以拿到。下面的方法反而拿不到。

        console.log( $('#checkbox').attr('checked') );

参考博客

猜你喜欢

转载自www.cnblogs.com/xguoz/p/11650371.html