0

我正在尝试使用 knockout.js 创建一个按钮来创建文本我不确定是 html 还是 javascript 有问题,但控制台说的是我的“开始”ID 的 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);
         }
     });

此外,我的表单标签不会采取 action="none",而且问题不是 onclick,而是 getElementById。

4

3 回答 3

3

Javascript 是区分大小写的,所以.onClick应该是.onclick

于 2013-06-28T17:15:15.757 回答
0

你可以这样做:

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

或者

进一步这样:

<button id="answerSubmit" value="submit" onclick="questionare()">submit</button>
function questionare(){
    ..............Rest of code.........
}
于 2013-06-28T17:18:12.283 回答
-2

That's the problem when people who doesn't know javascript programming learns a really tiny bit of programing on jQuery.

If you want your code to work, read about event listeners or direct event assignation, in this case you need to assign the function to the handler, like this:

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

But there's the catch, when you try to assign an event handler like this, you can't send any parameter, and the only parameter received is the event object that is generated magically when an event is triggered

于 2013-06-28T17:19:03.733 回答