简体   繁体   English

JavaScript和复选框无效

[英]JavaScript and Check Boxes not working

Basically I am trying to make a form in HTML that uses JavaScript to take input from check boxes and open a specific page on my site depending on which boxes are checked. 基本上我试图在HTML中创建一个表单,使用JavaScript从复选框中获取输入并在我的网站上打开特定页面,具体取决于选中的框。

When I check a box or don't check a box or more it only responds by giving the alert please check a box. 当我选中一个方框或不选中一个或多个方框时,它只会通过发出提醒来回复,请选中一个方框。 Does anyone have any idea on what I am missing? 有没有人知道我错过了什么? This is currently how I am writing the code after trying multiple different ways. 这是我在尝试多种不同方式后编写代码的方式。 Also note that I have in the same folder html pages for the specified windows I am trying to open. 另请注意,我在同一个文件夹中有html页面,我正在尝试打开指定的窗口。

JavaScript: JavaScript的:

function checking()
{
    var frm = document.diaglab;
    var majdep1 = document.getElementById("majdep").checked;
    var bipolar1 = document.getElementsByName("bipolar").checked;

    if(majdep1.checked == true && bipolar1.checked == true)
    {
        window.alert("possible bipolar disorder");
        window.open("bipolar1.html");
    }else{
        window.alert("please check boxes");
    }
}

HTML code: HTML代码:

<!DOCTYPE HTML> 
<html>
<head>
<title></title>
<meta name="" content="">
<link href="illness.css" rel="stylesheet">
<script type="text/javascript" src="checked.js"></script>


</head>
<body>
<div id="container">
<div >
    <h1 id="mainhead">Exploring Symptoms</h1>
</div>
<div>
    <article ><p>This exploring area is just a lab tool that allows you too explore how mental illness is diagnosed<br />
    according to the DSM V. You can click on certain symptoms and see what diagnoses may be applied. It<br />
    is important to know that this is not an actual diagnosis tool and should not be applied to your life in any<br />
    way as to self diagnose, treat or medicate yourself or anyone else. If you feel that you may be suffering<br />
    from a mental illness contact your physician! Also note that this tool is not all inclusive and if you want<br />
     a deeper understanding please refer to the DSM V.</p></article>
</div>
<hr>

<div id="explrfrm">
    <form name="diaglab"  method="post" onsubmit="return checking()">
    <label id="explrlab">Depressive and Manic Symptoms</label><br />
    <span id="explrlab1">
        <label id= title="Symptom1" >1. Depressed mood (sad, empty, hopeless)most of the day, nearly everyday.</label> <input type="checkbox" name="majdep" id="majdep" ><br /><br />
        <label  title="Symptom2">2. Diminished interest or pleasure in all or nearly all activities</label> <input type="checkbox" name="majdep" id="majdep"><br /><br />
        <label  title="Symptom3">3. Significant weight loss or gain (without dieting) or decreased appetite most days.</label> <input type="checkbox" name="majdep" id="majdep"><br /><br />
        <label  title="Symptom4">4. Insomnia (inability to sleep) or Hypersomnia (sleeping too much) nearly everyday</label> <input type="checkbox" name="majdep" id="majdep"><br /><br />
        <label title="Symptom5">5. Fatigue or loss of energy almost everyday</label> <input type="checkbox" name="majdep" id="majdep"><br /><br />
        <label  title="Symptom6">6. Feelings of worthlessness or excessive and inappropriate guilt.</label> <input type="checkbox" name="majdep" is="majdep"><br /><br />
        <label  title="Symptom7">7. Diminished ability to think, concentrate, or indecisiveness.</label> <input type="checkbox" name="majdep" id="majdep"><br /><br />
        <label  title="Symptom8">8. Recurrent thoughts of death, suicidal ideations with or without a plan and or attempt.</label> <input type="checkbox" name="majdep" id="majdep"><br /><br />
        <label  title="Symptom9">9. significant impairment  in social , occupational, or other important areas of functioning</label> <input type="checkbox" name="majdep" id="majdep"><br /><br />
        <label title="Symptom10">10. Distinct period of abnormally or persistent elevated, expansive, or irritable mood and increased goal directed energy for at least one week all day or nearly all day</label> <input type="checkbox" name="bipolar" id="bipolar"><br /><br />
        <label  title="Symptom11">11. During the period of increased mood energy at least three of the following: inflated self esteem, dcreased need for sleep, extreme talkitivity, flight of ideas,distractibility, increased goal directed activity, or excessive activity involvement nearly everyday.</label> <input type="checkbox" name="bipolar" id="bipolar"><br />
        </span>

        <input type="submit" value="submit">
        <input type="reset" value="Reset">


    </form>

</div>



</div>

</body>
</html>

You have an error in your code 您的代码中有错误

You have referenced .checked when you define the variables, and again when you do the if statement. 在定义变量时引用了.checked ,在执行if语句时再次引用.checked

The following should work 以下应该有效

function checking()
{
    var frm = document.diaglab;
    // This is incorrect! You have .checked at the end!
    //var majdep1 = document.getElementById("majdep").checked;
    //var bipolar1 = document.getElementsByName("bipolar").checked;
    var majdep1 = document.getElementById("majdep");
    var bipolar1 = document.getElementsById("bipolar");

    if(majdep1.checked == true && bipolar1.checked == true)
    {
        window.alert("possible bipolar disorder");
        window.open("bipolar1.html");
    }else{
        window.alert("please check boxes");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM