How to tell if a checkbox is checked?

问题: If I have a checkbox and decide I need for it to throw a boolean when it is clicked, how would I do that? Would I use the onclick method or would I use data-js to change th...

问题:

If I have a checkbox and decide I need for it to throw a boolean when it is clicked, how would I do that? Would I use the onclick method or would I use data-js to change the var? I'm new to programming and am confused.

Checkbox:

<input id="private-player" type="checkbox" style="margin-right: 5px;" />

回答1:

Call

function testCheckbox(cb) {
    // cb.checked is Boolean
    if (cb.checked) alert("checkbox is checked");
    else alert("checkbox is NOT checked");
};

From

<label>click to check/uncheck: </label><input 
   type="checkbox" onchange="testCheckbox(this);" /> 
  • 发表于 2019-03-04 16:39
  • 阅读 ( 167 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除