表格在同一页面上显示结果+随机答案

时间:2014-10-10 21:15:35

标签: javascript

所以我有一个3个问题"你在哪个专业测验" ...在选择并提交了文本,广播和选择之后,结果显示在他们旁边。文本和选择但不是收音机。选择显示我是否有无线电,if ((php.checked==false) && (asp.checked==false) && (js.checked==false)){rad.setAttribute("style", "display:inline");} ...我的设置现在如何显示文本,但收音机并选择不。此外,我需要一个包含解决方案的数组,一旦答案提交,它就会随机显示一个解决方案,例如"你是一个战士!"。这是.PHP扩展。继承我的代码:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Ex 2</title>
<link href="styles.css" rel="stylesheet">
<script>
function checkForm(){

    var chk = true;
    var mName = document.getElementById("txtMeth");
    var meth = document.getElementById("methMess");
    var rad = document.getElementById("radMess");
    var sel = document.getElementById("selMess");
    var php = document.getElementById("a");
    var asp = document.getElementById("b");
    var js = document.getElementById("c");
    var arr = document.getElementById("imp");

        mName.style.backgroundColor="#fff";
        meth.setAttribute("style", "display:none");
        rad.setAttribute("style", "display:none");
        sel.setAttribute("style", "display:none");


    if (mName.value=='no'){     
        document.getElementById("methMess").innerHTML = "No";
        meth.setAttribute("style", "display:inline");
        chk = false;
    }
    if (mName.value=='yes'){        
        document.getElementById("methMess").innerHTML = "Yes";
        meth.setAttribute("style", "display:inline");
        chk = false;
    }       
    if (php.value==A){
        document.getElementById("radMess").innerHTML = "Healer";
        rad.setAttribute("style", "display:inline");
        chk = false;
    }
    if (asp.value==B){
        document.getElementById("radMess").innerHTML = "Dark";
        rad.setAttribute("style", "display:inline");
        chk = false;
    }
    if (js.value==C){
        document.getElementById("radMess").innerHTML = "One with the Elements";
        rad.setAttribute("style", "display:inline");
        chk = false;
    }
    if (arr.value==1){
        document.getElementById("selMess").innerHTML = "Rifle";
        sel.setAttribute("style", "display:inline");
        chk = false;
    }
    if (arr.value==2){
        document.getElementById("selMess").innerHTML = "Bown and Arrow";
        sel.setAttribute("style", "display:inline");
        chk = false;
    }
    if (arr.value==3){
        document.getElementById("selMess").innerHTML = "Daggers";
        sel.setAttribute("style", "display:inline");
        chk = false;
    }

}

</script>
</head>

<body>
<div class="page">   
<main role="main">
<article>
    <div id="errMess" class="errMess">*Required Fields Missing</div>
      <h1>What Guild Wars 2 Profession Are You</h1>

        <div class="cssTable" style="margin-top:-25px;">
        <form method="post">
          <table>
          <tr><td colspan="3"></td></tr>
            <tr>
            <td><div align="right">Do you like to do high damage?</div></td><td width="217">
            <input id="txtMeth" name="txtMeth" type="text" size="25"></td><td ><div id="methMess" style="display:none"></div></td></tr>
            <tr>
                <td><div align="right">What best describes you?</div></td><td>
                <input id="a" type="radio" name = "group1" value="A">Healer</input>
                <input id="b" type="radio" name = "group1" value="B">Dark</input>
                <input id="c" type="radio" name = "group1" value="C">Earthling</input>               
                </td><td><div id="radMess" style="display:none"></div></td>
                </tr>
                <tr>
                <td>What weapon would you like to have?</td>
                <td>
                <select id="imp"><option value="0" selected="true">Select One</option>
                <option value="1">Rifle</option>
                <option value="2">Bow and Arrow</option>
                <option value="3">Daggers</option></select>
                </td><td><div id="selMess" style="display:none"></div></td>
                </tr>
                <tr><td colspan="3" align="right"><input type="button" class="styled-button-7" value="Send" onclick="checkForm()"/></td></tr></table></form></div></article>
                </main></div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

就像这样,您的单选按钮将始终具有值&#39; A&#39;,&#39; B&#39;或者&#39; C&#39;,它只是您在HTML中定义它们的方式。

应使用&#39; .checked&#39;检查无线电按钮是否有效。财产而不是价值。

请参阅js fiddle here

我对您的代码所做的更改是:

我更新了你的if

if (php.checked) {
    document.getElementById("radMess").innerHTML = "Healer";
    rad.setAttribute("style", "display:inline");
    chk = false;
}
if (asp.checked) {
    document.getElementById("radMess").innerHTML = "Dark";
    rad.setAttribute("style", "display:inline");
    chk = false;
}
if (js.checked) {
    document.getElementById("radMess").innerHTML = "One with the Elements";
    rad.setAttribute("style", "display:inline");
    chk = false;
}

答案 1 :(得分:0)

所以......我不得不重命名一切,因为我一直感到困惑。不要只是将其打开或完全复制......你的老师可能会知道你没有这样做。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Ex 2</title>
<link href="styles.css" rel="stylesheet">

<style>td { height:25px }</style>
<script>

function checkForm() {

   var high   = document.getElementById('high');      // mName
   var desc   = document.getElementsByName('desc');   // group1
   var weapon = document.getElementById('weapon');    // imp

   // console.log(high);
   // console.log(desc);
   // console.log(weapon);

   // get the checked radio button value to be used later
   var desc_value;  
   for(var i=0; i<desc.length; i++){ 
      if (desc[i].checked) desc_value = desc[i].value;
   }

   var error = false;
   if (!high.value) {
      // if you need to do validation for Yes/No, do it here 
      document.getElementById('errMess').innerHTML = 'high damage missing'; 
      error = true; 
   }
   else if (!desc_value) {
      document.getElementById('errMess').innerHTML = 'desc missing'; 
      error = true; 
   }
   else if (!weapon.value) {
      document.getElementById('errMess').innerHTML = 'weapon missing'; 
      error = true; 
   }

   if (!error) {
      document.getElementById('errMess').innerHTML = ''; 

      // do you like to do high damage?
      high.style.background = '#fff';
      high.style.display    = 'none';
      var highMess = document.getElementById('highMess');
          highMess.innerHTML = high.value;
          highMess.setAttribute('style', 'display:inline');  

      // what best describes you?
      var desc_val;
      var desc_msg = document.getElementById('descMsg');
          desc_msg.setAttribute('style', 'display:inline'); 
          desc_msg.innerHTML = desc_value; 
      for(var i=0; i<desc.length; i++){    
         // loop through the radio button group to hide the parent <span>
         desc[i].parentNode.setAttribute('style', 'display:none'); 
      }

      // what weapon would you like to have? 
      weapon.style.display = 'none';
      var weapon_val = weapon.options[weapon.selectedIndex].value;
      var weapon_msg = document.getElementById('weaponMsg');
          weapon_msg.innerHTML = weapon_val;
          weapon_msg.setAttribute('style', 'display:inline'); 
   }
}

</script>
</head>

<!-- this sets the selected option to being out of range (aka blank/empty) -->    
<body onload="document.getElementById('weapon').selectedIndex = -1;">

<div class="page">
<main role='main'>
<article>
   <div id='errMess' class='errMess' style='font-weight:bold; height:26px'></div>
     <h1>What Guild Wars 2 profession are you.....?</h1>
      <div class='cssTable' style='margin-top:-25px;'>
      <form method='post'>
        <table>
         <tr><td colspan='3'></td></tr>
         <tr>
            <td><div align='right'>Do you like to do high damage?</div></td>
            <td width='217px'><input id='high' name='high' type='text' size='25'/></td>
            <td><div id='highMess' style='display:none'></div></td>
         </tr>
         <tr>
            <td><div align='right'>What best describes you?</div></td>
            <td>
               <!-- i added spans outside of the <input/> so you could hide the text easily, too -->
               <span><input id='healer'    type='radio' name='desc' value='healer'>Healer</input></span>
               <span><input id='dark'      type='radio' name='desc' value='dark'>Dark</input></span>
               <span><input id='earthling' type='radio' name='desc' value='earthling'>Earthling</input></span>
            </td>
            <td><div id='descMsg' style='display:none'></div></td>
         </tr>
         <tr>
            <td>What weapon would you like to have?</td>
            <td>
               <select id='weapon' name='weapon'>
                  <option value='rifle'>Rifle</option>
                  <option value='bow_arrow'>Bow and Arrow</option>
                  <option value='daggers'>Daggers</option>
               </select>
            </td>
            <td><div id='weaponMsg' style='display:none'></div></td>
         </tr>
         <tr>
            <td colspan='3' align='right'><input type='button' class='styled-button-7' onClick='checkForm()' value='Submit'/></td>
         </tr>
         </table>
      </form>
      </div>
   </div>
</article>
</main>
</body>
</html>