1

基本上,我使用引导程序作为我的 CSS,但我遇到了问题。当我单击按钮 ( id="myQuestionButton") 时,我希望它从输入框 ( id="myInput") 中获取输入并运行它function checkQuestionWords()

这是与输入和按钮有关的代码块:

<div class="input-append span9 offset3">
  <form>
    <input class="span4" type="text" id="myInput" 
    placeholder="Enter your question."></input>
    <button class="btn" type="submit" id="myQuestionButton">
    Let's check second.</button>
  </form>
</div>

这是它将打印初步结果的地方(现在只是在 a 中<div> </div>):

<div id="results"></div>

到目前为止,这是我的功能:

function checkQuestionWords(){
  var theInput = document.getByElementId("myInput");
  var theQuestion = theInput.trim();
  var questionWords = ["who", "what", "where", "when", "why", "how"];
  var isItGood = false;

for (var i=0, i<7; i++){
  var holder1 = questionWords[i];
  var holder2 = holder1.length;
    if (theQuestion.substr(0,holder2) == questionWords[i]){
      isItGood = true;
      break;}
    else{
      isItGood = false;}}

if (isItGood == false){
  document.getByElementId("results") = "Please enter a question...";}
  //the above just reminds them to begin with a question word; see questionWords
else{
  document.getByElementId("results") = "all is good";}

}

我试着在onclick=checkQuestionWords()"里面做一个整体,<button ...></button>但由于某种原因没有奏效。

4

3 回答 3

1

你的问题只是你正在使用 var theInput = document.getByElementId("myInput");吗?

这将为您提供带有myInputid的输入控件,而不是其中的文本。你想要更多类似的东西

var theInput = document.getByElementId("myInput").value;

这将为您提供实际文本,然后您可以分配给theQuestion使用

var theQuestion = theInput.trim();
于 2013-06-24T03:24:26.353 回答
0

尝试这样的简单调用:

var btn = document.getElementById["myQuestionButton"];
btn.onClick = checkQuestionWords;
于 2013-06-24T03:30:30.073 回答
0

上面的代码有很多问题:

问题是:

  1. getByElementId应该getElementById
  2. document.getByElementId("myInput")应该document.getElementById("myInput").value;
  3. document.getByElementId("results")应该document.getElementById("results").innerHTML

请参阅下面的工作代码:

<script>
function checkQuestionWords() {
    var theInput = document.getElementById("myInput").value;
    var theQuestion = theInput;//.trim();
    var questionWords = ["who", "what", "where", "when", "why", "how"];
    var isItGood = false;


    for (var i=0; i<7; i++){
      var holder1 = questionWords[i];
      var holder2 = holder1.length;
        if (theQuestion.substr(0,holder2) == questionWords[i]){
          isItGood = true;
          break;}
        else{
          isItGood = false;}}

    if (isItGood == false){
      document.getElementById("results").innerHTML = "Please enter a question...";
    }
      //the above just reminds them to begin with a question word; see questionWords
    else{
      document.getElementById("results").innerHTML = "all is good";
    }

    return false;

}
</script>
<div class="input-append span9 offset3">

    <input class="span4" type="text" id="myInput" 
    placeholder="Enter your question."></input>
    <button class="btn" type="submit" id="myQuestionButton" onclick="checkQuestionWords();">
Let's check second.</button>

</div>
<div id="results"></div>
于 2013-06-24T03:33:59.803 回答