2015-08-19 160 views
3

基本上我試圖製作一個HTML格式的表單,它使用JavaScript從複選框中接受輸入並在我的網站上打開特定的頁面,具體取決於檢查哪些複選框。JavaScript和複選框不起作用

當我檢查一個盒子或不檢查一個盒子或更多時,它只響應提醒,請檢查一個盒子。有沒有人有任何想法我缺少什麼?這是目前我嘗試多種不同方式後編寫代碼的方式。另外請注意,我在同一個文件夾的html頁面中顯示了我想要打開的指定窗口。

的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代碼:

<!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> 
+1

請正確格式化您的代碼,你檢查控制檯? *提示:*'document.getElementsByName'返回一個'array'。 – Script47

+0

它看起來像你剛剛從按鈕打開'window.open'頁面,爲什麼它是一個提交?另外,如果我檢查多個框?一般來說,我很困惑這個界面。你應該做一個jsfiddle,並具體說明你期望的行爲。 – Marc

+1

你的頁面只能有一個真正的'id =「majdep」',在大多數瀏覽器中它是第一個,但之後的任何東西都不會被包含。 –

回答

3

您有一個錯誤在你的代碼

你有參考.checked當你定義變量,並再次當你做如果聲明。

下應該工作

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"); 
    } 
} 
+0

我從變量中刪除了.checked但沒有改變腳本的行爲。我也將var bipolar1更改爲document.getElementById而不是ByName。 –

+0

其實猜猜你是否真棒,因爲現在它在我將第二個變量改爲ById並刪除.checked之後正在工作。它必須花一秒鐘才能真正意識到這一變化。謝謝你的幫助。我可以問你另一個問題,如果我想做的代碼爲getElementsByName什麼是適當的格式爲JavaScript中的數組?如果我問的是有道理的。我早些使用for循環和if語句嘗試了這種方式,但它不起作用。 –

+0

我更新了我的代碼以修復該問題;)首先,您將遇到問題,因爲您有多個具有相同ID的HTML元素。你需要解決這個問題。至於getElementsByName,它返回一個DOM元素的NodeList。你可以通過爲for循環獲得單個元素(for i = 0; i