2013-06-28 12 views
0

我试图让一个按钮来创建一个使用文本knockout.js我不知道它这是使用问题的HTML或JavaScript,但什么控制台说是否是我的“开始”的onclick id为null 。为什么我的onClick无法正常工作?

<div style="margin:0 20px 0 20px;" > 
     <p data-bind="text: currentQuestion"></p> 
     <form id="discoutQuestions" action="none"> 
     <button id="start" value="start">start</button> 
     <label>Answer:</label> 
     <input type="text" id="answer"/> 
     <button id="answerSubmit" value="submit" onclick="questionare()">submit</button> 
     </form> 
    </div> 




    document.getElementById('start').onClick(ko.applyBindings(questionList)); 
    document.getElementById('answerSubmit').onClick(function questionare() 
    { 
     var correct=0; 
     var count=0; 
     var questionList= new questionViewModel(); 
     function next() 
     { 
      questionList.setQuestion(questionList.getNext()); 
      ko.applyBindings(questionList); 
     } 
     var answer= document.getElementById('answer').value; 
     if(answer==questionList.answers[0][1]&&count!=questionList.getLength()) 
     { 
      correct++; 
      count++; 
      next(); 
     } 
     else if(answer!=questionList.answers[0][1]&&count!=questionList.getLength()) 
     { 
      count++; 
      next(); 
     } 
     else 
     { 
      react= new message(); 
      if(correct/count>.75) 
      { 
        react.setQuestion("Congradualtions! You have won a discount."); 
      } 
      else{ 
        react.setQuestion("We are sorry, you did not answer enough questions right ofr a discount."); 
      } 
      ko.applyBindings(react); 
     } 
    }); 

此外,我的表单标签不会采取行动=“none”和它不是,这是一个问题的onclick,它的的getElementById。

+0

的onclick事件所产生的神奇事件对象不是错误,它的的getElementById即返回null –

回答

0

你可以这样做:

document.getElementById('answerSubmit').onclick = function questionare(){} 

或者

进一步是这样的:

<button id="answerSubmit" value="submit" onclick="questionare()">submit</button> 
function questionare(){ 
    ..............Rest of code......... 
} 
3

JavaScript是大小写敏感的,所以.onClick应该.onclick

-2

那些不懂JavaScript编程的人会在jQuery上学到很少的编程问题。

如果你希望你的代码工作,了解事件侦听器或直接活动分配,在这种情况下,你需要的功能分配给处理器,就像这样:

document.getElementById('start').onClick = ko.applyBindings; 

但有渔获,当你尝试分配一个事件处理这样,你不能发送任何参数,并获得唯一的参数是在触发

相关问题