2017-07-29 68 views
0

如果複選框被選中,我無法通過一組文本。下面的JS和HTML ... Prob一個簡單的修復 - 但我失去了這不起作用。js HTML複選框

// JS

function getTrainerExp() 

{ 
    var trainerexp="None"; 
    //Get a reference to the form id="courseform" 
    var theForm = document.forms["courseform"]; 
    //Get a reference to the checkbox id="includeexp" 
    var includeexp = theForm.elements["includeexp"]; 

    //If they checked the box set trainerexp to ACMEEXP 
    if(includeExp.checked==true) 
    { 
     trainerexp="ACMEXP"; 
    } 
    //return the trainer expenses 
    return trainerexp; 
} 


function calculateExp() 
{ 
    var expense = getTrainerExp() ; 

    //display the result 
    var divobj = document.getElementById('trainerexp').checked; 

    divobj.style.display='block'; 
    divobj.innerHTML = "Please Include " +expense; 

} 

HTML

<div id="wrap"> 
<form action="" id="courseform" onsubmit="return false;"> 
<div> 
<p> 
<label for='includeexp' class="inlinelabel">If course delivery is at customer site include trainer travel costs and expenses</label> 

<input type="checkbox" id="includeexp" name='includeexp' onclick="calculateExp()" /> 
</p> 

<div id="trainerexp"></div> 

CSS條目trainerexp

#wrap div#trainerexp 
{ 
    padding:10px; 
    font-weight:bold; 
    background-color:#ff0; 
} 

回答

1

爲什麼不使用的document.getElementById?

function getTrainerExp() 
 

 
{ 
 
    var trainerexp="None"; 
 
    //Get a reference to the form id="courseform" 
 
    var theForm = document.forms["courseform"]; 
 
    //Get a reference to the checkbox id="includeexp" 
 
    var includeExp = document.getElementById('includeexp'); 
 

 
    //If they checked the box set trainerexp to ACMEEXP 
 
    if(includeExp.checked==true) 
 
    { 
 
     trainerexp="ACMEXP"; 
 
    } 
 
    //return the trainer expenses 
 
    return trainerexp; 
 
} 
 

 

 
function calculateExp() 
 
{ 
 
    var expense = getTrainerExp() ; 
 

 
    
 
    document.getElementById("trainerexp").innerHTML = "Please Include " +expense; 
 

 
}
#wrap div#trainerexp 
 
{ 
 
    padding:10px; 
 
    font-weight:bold; 
 
    background-color:#ff0; 
 
}
<div id="wrap"> 
 
<form action="" id="courseform" onsubmit="return false;"> 
 
<div> 
 
<p> 
 
<label for='includeexp' class="inlinelabel">If course delivery is at customer site include trainer travel costs and expenses</label> 
 

 
<input type="checkbox" id="includeexp" name='includeexp' onclick="calculateExp()" /> 
 
</p> 
 

 
<div id="trainerexp"></div>

+0

這工作 - 謝謝你的職位 - 非常感謝! :) – DudeNamedBen

+0

不客氣。如果你找到了這個正確的答案,你可以將其標記爲答案。 – zennith