如何检查是否选中了复选框?

时间:2012-03-27 10:04:12

标签: javascript jquery-mobile checkbox

我正在使用jQuery Mobile构建移动Web应用程序,我想检查是否选中了复选框。这是我的代码。

<script type=text/javascript>
  function validate(){
    if (remember.checked == 1){
      alert("checked") ;
    } else {
      alert("You didn't check it! Let me check it for you.")
    }
  }
</script>

<input id="remember" name="remember" type="checkbox" onclick="validate()" />

但由于某种原因,它不会执行它。

请帮忙!

---- ----- EDIT 这就是我现在所拥有的。

<DIV data-role="content" data-theme="g">
    <DIV class=ui-grid-g-login>
        <FORM method=post action=[$=PROBE(266)/] data-theme="C">
            <P>~DATA_ERROR~</P>
            <div id="mail" data-role="fieldcontain">
                <label for="mail">Email:*</label>       
                <input id="mail" name="mail" type="email" />
            </div>
            <div id="pass" data-role="fieldcontain">
                <label for="pass">Paswoord:*</label>        
                <input id="pass" name="pass" type="password" />
            </div>
            <div id="remember" data-role="fieldcontain">
                <label for="remember">Onthoud mij</label>       
                <input id="remember" name="remember" type="checkbox" onclick="validate()" />
            </div>
            <P><INPUT class=btn name=submit value=Login type=submit  onclick="validate()"></P>  
        </FORM>
    </DIV>
</DIV><!-- /content -->

<script type=text/javascript>
function validate(){
    var remember = document.getElementById('remember');
    if (remember.checked){
        alert("checked") ;
    }else{
        alert("You didn't check it! Let me check it for you.")
    }
}
</script>

----编辑 -

解决了它,问题是fieldcontain也被命名为'remember'

15 个答案:

答案 0 :(得分:240)

checkedboolean属性,因此您可以在IF条件下直接使用它: -

 <script type="text/javascript">
    function validate() {
        if (document.getElementById('remember').checked) {
            alert("checked");
        } else {
            alert("You didn't check it! Let me check it for you.");
        }
    }
    </script>

答案 1 :(得分:50)

试试这个:

function validate() {
  var remember = document.getElementById("remember");
  if (remember.checked) {
    alert("checked");
  } else {
    alert("You didn't check it! Let me check it for you.");
  }
}

您的脚本不知道变量remember是什么。您需要首先使用getElementById()获取元素。

答案 2 :(得分:12)

这可以让您检查id='remember'元素是否为'checked'

if (document.getElementById('remember').is(':checked')

答案 3 :(得分:8)

如果您将此表单用于移动应用,那么您可以使用必需的属性html5。 你不想为此使用任何java脚本验证。它应该工作

<input id="remember" name="remember" type="checkbox" required="required" />

答案 4 :(得分:5)

像这样使用

<script type=text/javascript>
function validate(){
if (document.getElementById('remember').checked){
          alert("checked") ;
}else{
alert("You didn't check it! Let me check it for you.")
}
}
</script>

<input id="remember" name="remember" type="checkbox" onclick="validate()" />

答案 5 :(得分:3)

    if (document.getElementById('remember').checked) {
        alert("checked");
    }
    else {
        alert("You didn't check it! Let me check it for you.");
    }

答案 6 :(得分:3)

这应该有效

    function validate() {
        if ($('#remeber').is(':checked')) {
            alert("checked");
        } else {
            alert("You didn't check it! Let me check it for you.");
        }
    }

答案 7 :(得分:2)

remember未定义... checked属性是布尔值而不是数字。

function validate(){
    var remember = document.getElementById('remember');
    if (remember.checked){
        alert("checked") ;
    }else{
        alert("You didn't check it! Let me check it for you.")
    }
}

答案 8 :(得分:2)

我正在使用它,它适用于 Jquery

<强> Jquery的:

var checkbox = $('[name="remember"]');

if (checkbox.is(':checked'))
{
    console.log('The checkbox is checked');
}else
{
    console.log('The checkbox is not checked');
}

非常简单,但是工作。

问候!

答案 9 :(得分:0)

您可以尝试以下方法:

{ data: post }

答案 10 :(得分:0)

使用以下简单代码: https://jsfiddle.net/Divyesh_Patel/v7a4h3kr/7/

# loop for calculating the density function
for(j in 2:length(full_x))
{

# define the normal density
trued<-function(y){dnorm(full_x[j],mean=y*full_x[j-1],sd=true_eps)}

# joint distribution
# which is normal density multiplied by a piror
if(j>=3)
{
  trued<- function(y){trued(y)**true_density(y)}
}

# integration of the density w.r.t. rho
trueint<-integrate(trued, lower = 0, upper = 1)$value

# density function 
true_density<-function(y) {trued(y)/trueint}

# save into list
dyn_density_true[[j-1]]<-true_density
}

答案 11 :(得分:0)

使用以下简单代码: https://jsfiddle.net/Divyesh_Patel/v7a4h3kr/7/

<input type="checkbox" id="check">
<a href="#" onclick="check()">click</a>
<button onclick="check()">button</button>
<script>
 function check() {
    		if (document.getElementById('check').checked) {
            alert("checked");
        } else {
            alert("Not checked.");
        }
       
    }

</script>

答案 12 :(得分:0)

//HTML
<input type="checkbox" id="someID">

// JavaScript
const someCheckbox = document.getElementById('someID');

someCheckbox.addEventListener('change', e => {
  if(e.target.checked === true) {
    conlose.log("Checkbox is checked - boolean value: ", e.target.checked)
  }
if(e.target.checked === true) {
    conlose.log("Checkbox is not checked - boolean value: ", e.target.checked)
  }
});

know more

答案 13 :(得分:0)

remember 变量未定义。所以试试这个:

function validate() {
  var remember = document.getElementById("remember");
  if (remember.checked) {
    alert("checked");
  } else {
    alert("You didn't check it! Let me check it for you.");
  }
}

答案 14 :(得分:-1)

试一下

<script type="text/javascript">
window.onload = function () {
    var input = document.querySelector('input[type=checkbox]');

    function check() {
        if (input.checked) {
            alert("checked");
        } else {
            alert("You didn't check it.");
        }
    }
    input.onchange = check;
    check();
}
</script>