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/

相关文章:

C可变参数

具有子模块和函数的 Python 模块

javascript - 如何为 JavaScript 创建的每个 DIV 制作不同的 insideText?

javascript - LESS - 在客户端设置变量

javascript - Angular 在 Angular 动画中未定义

algorithm - 根据位置计算组合

python - 在__main__中调用While循环函数吗?

javascript - BlackBerry 10 浏览器在使用 preventDefault 时使用浏览器提交按钮提交表单

javascript - 通过 Word API 获取当前段落的样式/字体(字体大小、颜色等)

javascript - 键映射在 JavaScript 映射中的工作原理