基本上,我使用 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");
?
这将为您提供带有 myInput 的 id
的输入控件,但不包含其中的文本。你想要的东西更像
var theInput = document.getByElementId("myInput").value;
这将为您提供实际文本,然后您可以使用
将其分配给theQuestion
var theQuestion = theInput.trim();
关于javascript - 当我单击按钮时需要通过函数运行 <input> (使用 Bootstrap ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17267526/