简体   繁体   English

onclick()操作未响应

[英]onclick() action is not responding

i 'm creating a survey-like page. 我正在创建一个类似调查的页面。 I use three basic javascript functions, goNext(i) which hides a certain div tags and shows the next, goPrevious(i) , which pretty much does the same but backwards, and answerExists(questionNumber) which checks if all radio buttons are checked. 我使用了三个基本的javascript函数: goNext(i)它隐藏了一个div标签并显示了下一个), goPrevious(i) (几乎相同但向后)和answerExists(questionNumber)它检查是否选中了所有单选按钮)。 From the point i created answerExists(), goNext() and i suppose goPrevious() too, stopped responding. 从我创建answerExists(), goNext()到我也想goPrevious()一点,都停止了响应。 Maybe it's something to do with the onsubmit() function, but i cant figure it out. 也许这与onsubmit()函数有关,但我无法弄清楚。 These are the Javascript functions: 这些是Javascript函数:

function goNext(i) {
  document.getElementById("div" + i).style.display = 'none';
  document.getElementById("div" + (i + 1)).style.display = 'block';
}

function goPrevious(i) {
  document.getElementById("div" + i).style.display = 'none';
  document.getElementById("div" + (i - 1)).style.display = 'block';
}

function checkAnswers(questions) {
  var answers = new Array(questions);
  for(var i = 1; i <= questions; i++){
    answers[i-1] = false;
    var radio = document.getElementsByClassName('Radio' + i);
    for (var j=0; j < radio.length; j++) {
      if(radio[j].checked == true)
         alert(radio[j].value)answers[i-1] = true;
    }
    for (var i = 0; i < questions; i++){
      if(answers[i] == false){
        alert("Please answer all questions.");
        return false;
      }   
    }
    return true;
}

And this is the HTML form: 这是HTML形式:

<form id="content" action="getresults.jsp" method="post" onsubmit="return checkAnswers(2);">
    <div id="div1" style="display: block;">
        <strong>1. Question 1</strong><br><br>&nbsp;&nbsp;
        <input id="radio1" class="radio1" name="radio1" onclick="if (this.__chk) this.checked = false" onmousedown="this.__chk = this.checked" type="radio" value="Answer1" /> 
        Answer1<br><br><br><br>
        <div class="auto-style1">
            <input name="next" onclick="goNext(1);" type="button" value="Next" />
        </div>
    </div>
    <div id="div2" style="display: none;">
        <strong>2. Question 2</strong><br><br>&nbsp;&nbsp;
        <input id="radio2" class="radio2" name="radio2" onclick="if (this.__chk) this.checked = false" onmousedown="this.__chk = this.checked" type="radio" value="Answer 2" /> 
        Answer 2<br><br>&nbsp;&nbsp;
        <input id="radio2" class="radio2" name="radio2" onclick="if (this.__chk) this.checked = false" onmousedown="this.__chk = this.checked" type="radio" value="dfgdgfdgf" /> 
        dgfgddgf<br><br><br><br>
        <div class="auto-style1">
            <input name="previous" onclick="goPrevious(2);" type="button" value="Previous" />&nbsp;
            <input name="submit" type="submit" value="Submit" />
        </div>
    </div>
</form>

It is actually all created dynamically, but still, it worked before i added the checkAnswers() function. 它实际上都是动态创建的,但是在我添加checkAnswers()函数之前,它仍然有效。

The problem is that you have syntax errors in your checkAnswers function, and the interpreter discards any scripts that contain syntax errors, so all your other functions get discarded, too. 问题是您的checkAnswers函数中存在语法错误,并且解释器会丢弃任何包含语法错误的脚本,因此所有其他函数也会被丢弃。

These are the two syntax errors: 这是两个语法错误:

  • alert(radio[j].value)answers[i-1] = true; is not a valid statement. 不是有效的声明。 (I'm guessing that you intended to delete the alert(radio[j].value) part, but highlighted the wrong thing?) (我猜您打算删除alert(radio[j].value)部分,但突出显示了错误的内容?)
  • You have more left-curly-braces { than right-curly-braces } . 您的左大括号{比右大括号}

Once you fix both of these issues, the script should at least run (though you may still have some debugging to do). 解决这两个问题后,脚本至少应运行 (尽管您可能仍需要进行一些调试)。

You are calling checkAnswers with a 2 as parameter and build an array with it. 您正在调用带有2作为参数的checkAnswers并使用它构建一个数组。 ???? ????

The code below will show the mistake. 下面的代码将显示错误。

try{
  //your code
}catch(er){
  alert(er);
}

I like to answer you step by step.... 我想逐步回答您。

1) Why do you really need to check whether answers for all questions are checked? 1)为什么您真的需要检查是否已检查所有问题的答案? I asked you this because radio buttons will have selected an option by default... or you can make an "active" thing for a particular option... By doing this you really don't need to check whether all questions are answered... since by default one answer is selected... 我问您这个问题是因为单选按钮默认情况下会选择一个选项...或者您可以为特定选项设置“活动”内容...通过这样做,您实际上不需要检查是否所有问题都得到了回答。 ..因为默认情况下选择了一个答案...

2) The click event doesnt work because it seems you don't have any "id" for input tags "next","previous","submit"... Have separate id's for each and capture the click event of that particular tag by using jquery... 2)cli​​ck事件不起作用,因为似乎您没有输入标签“ next”,“ previous”,“ submit”的任何“ id” ...每个都有单独的ID并捕获该特定标签的click事件通过使用jQuery ...

eg. 例如。 Have an id for next <input id="next"> 为下一个<input id="next">

$("#next").click(function() {
    //perform the action you need
  }

Do this for all click events.... 对所有点击事件执行此操作。

3) Finally... don't use input tags for elements you like to click... Try using button tags... 3)最后...不要对要单击的元素使用输入标签...请尝试使用按钮标签...

Hope it ll help you :)... 希望它能对您有所帮助:)...

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

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