猿问

如果选中复选框,如何检查?

如果选中复选框,如何检查?

我正在用jQueryMobile构建一个移动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()" />

但出于某种原因,它没有执行。

救命啊!

-编辑-这是我目前所拥有的。

<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>

-编辑

解决了这个问题,问题是字段包含也被命名为“记住”。


繁星淼淼
浏览 451回答 3
3回答

长风秋雁

checked是boolean属性,以便您可以直接在IF条件:-&nbsp;<script&nbsp;type="text/javascript"> &nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;validate()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(document.getElementById('remember').checked)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("checked"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("You&nbsp;didn't&nbsp;check&nbsp;it!&nbsp;Let&nbsp;me&nbsp;check&nbsp;it&nbsp;for&nbsp;you."); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;</script>

慕盖茨4494581

试试这个:function&nbsp;validate()&nbsp;{ &nbsp;&nbsp;var&nbsp;remember&nbsp;=&nbsp;document.getElementById("remember"); &nbsp;&nbsp;if&nbsp;(remember.checked)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;alert("checked"); &nbsp;&nbsp;}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;alert("You&nbsp;didn't&nbsp;check&nbsp;it!&nbsp;Let&nbsp;me&nbsp;check&nbsp;it&nbsp;for&nbsp;you."); &nbsp;&nbsp;}}您的脚本不知道变量是什么remember是。您需要首先使用getElementById()获取元素。

慕沐林林

像这样使用<script type=text/javascript>function validate(){if (document.getElementById('remember').checked){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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()" />
随时随地看视频慕课网APP
我要回答