Javascript 需要运行<;输入>;通过单击按钮时的函数(使用引导)

Javascript 需要运行<;输入>;通过单击按钮时的函数(使用引导),javascript,function,Javascript,Function,基本上,我使用引导作为CSS,但我遇到了一个问题。当我单击按钮(id=“myQuestionButton”)时,我希望它从输入框(id=“myInput”)获取输入,并通过函数checkQuestionWords()运行它 以下是与输入和按钮相关的代码块: <div class="input-append span9 offset3"> <form> <input class="span4" type="text" id="myInput" p

基本上,我使用引导作为CSS,但我遇到了一个问题。当我单击按钮(
id=“myQuestionButton”
)时,我希望它从输入框(
id=“myInput”
)获取输入,并通过
函数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>

让我们再看看。
这里是它将打印初步结果的地方(现在只在
中):


到目前为止,我的功能如下:

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";}
函数checkQuestionWords(){
var theInput=document.getByElementId(“myInput”);
var theQuestion=theInput.trim();
var questionWords=[“谁”、“什么”、“哪里”、“何时”、“为什么”、“如何”];
var isItGood=假;

对于(var i=0,i,您的问题就在于您正在使用吗
var theInput=document.getByElementId(“myInput”);

这将为您提供具有
id
myInput的输入控件,但不提供其中的文本

var theInput = document.getByElementId("myInput").value;
这将为您提供实际文本,然后您可以使用

var theQuestion = theInput.trim();

你的问题就是你正在使用的吗
var theInput=document.getByElementId(“myInput”);

这将为您提供具有
id
myInput的输入控件,但不提供其中的文本

var theInput = document.getByElementId("myInput").value;
这将为您提供实际文本,然后您可以使用

var theQuestion = theInput.trim();

尝试以下简单调用:

var btn = document.getElementById["myQuestionButton"];
btn.onClick = checkQuestionWords;

尝试以下简单调用:

var btn = document.getElementById["myQuestionButton"];
btn.onClick = checkQuestionWords;

上述代码中存在很多问题:

问题是:

  • getByElementId
    应该是
    getElementById
  • document.getByElementId(“myInput”)
    应该是
    document.getElementById(“myInput”).value;
  • document.getByElementId(“结果”)
    应该是
    document.getElementById(“结果”).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>
    
    
    函数checkQuestionWords(){
    var theInput=document.getElementById(“myInput”).value;
    var theQuestion=theInput;//.trim();
    var questionWords=[“谁”、“什么”、“哪里”、“何时”、“为什么”、“如何”];
    var isItGood=假;
    
    对于(var i=0;i而言,上述代码中存在许多问题:

    问题是:

  • getByElementId
    应该是
    getElementById
  • document.getByElementId(“myInput”)
    应该是
    document.getElementById(“myInput”).value;
  • document.getByElementId(“结果”)
    应该是
    document.getElementById(“结果”).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>
    
    
    函数checkQuestionWords(){
    var theInput=document.getElementById(“myInput”).value;
    var theQuestion=theInput;//.trim();
    var questionWords=[“谁”、“什么”、“哪里”、“何时”、“为什么”、“如何”];
    var isItGood=假;
    
    对于(var i=0;iPls显示异常一个小东西。输入框没有结束标记。请显示异常一个小东西。输入框没有结束标记。
    getElementById
    是一个函数,而不是对象。
    getElementById
    是一个函数,而不是对象。好的。当我把它放在空白页上时,这似乎起作用(带有指向CSS/html标题/正文等的适当链接),但当我将其与我的其余代码一起使用时,就没有了。我确实尝试过复制/粘贴它(并删除旧代码),但没有用。你能用新的更改编辑你的问题吗?这样我就可以查看和检查它了。Nvm,我让它工作了。问题似乎是,当数组中充满小写疑问词(“How”)时,问题将是大写(“How”)。我只是做了theQuestion=theQuestion.toLowerCase();一切都解决了。它在另一个页面上工作的原因是因为我一直用小写字母键入问题;我感到沮丧,所以我只是尽可能快地键入,从而忽略了shift的使用。好的。当我把它放在一个空白页面上(带有指向CSS/html标题/正文/等的适当链接)时,这似乎起到了作用,但当我将它与我的其他代码一起使用时,就不会了。我确实尝试过复制/粘贴它(并删除旧代码),但没有任何效果。你能用新的更改编辑你的问题吗?这样我就可以查看和检查它了。Nvm,我让它工作了。问题似乎是这个问题将是大写(“如何”)虽然数组中充满了小写疑问词(“how”)。我只是做了theQuestion=theQuestion.toLowerCase();一切都解决了。它在另一个页面上起作用的原因是因为我一直在用小写字母键入问题;我感到沮丧,所以我只是尽可能快地键入,从而忽略了shift的使用。