javascript - 当我单击按钮时需要通过函数运行 <input> (使用 Bootstrap )

标签 javascript function

基本上,我使用 bootstrap 作为 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>

这里将打印初步结果(目前仅在 <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>里面但由于某种原因这不起作用。

最佳答案

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

这将为您提供带有 myInputid 的输入控件,但不包含其中的文本。你想要的东西更像

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

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

将其分配给 theQuestion
var theQuestion = theInput.trim();

关于javascript - 当我单击按钮时需要通过函数运行 &lt;input&gt; (使用 Bootstrap ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17267526/

相关文章:

javascript - 为什么这个 Javascript 函数不显示 alert()?

javascript - 更新面板和 JavaScript Hack 中的重复按钮

c - 如何使用 FILE 作为 C 函数的参数?

javascript - 用户输入后加载 Material UI AutoComplete 建议

c# - 我如何只允许设置某些消息的用户 [NODE.JS]

delphi - Delphi中是否有*SysUtils.Format*的反函数

c - rand() 数字沉迷于 C 函数

swift - 使用默认值参数覆盖函数时,如何保持默认值与父类(super class)相同?

javascript - 在主线程上创建Imagebitmap?

javascript - 在 JavaScript 中解决冲突时出现意外结果