Javascript功能不会激活onClick [英] Javascript function will not activate onClick
问题描述
在我的页面头部:
In my page head:
<script>
function formula()
{
document.forms["inquire"]["search"].value.scrollIntoView();
}
</script>
在我的页面正文中:
<div id="nav">
<form id="inquire" name="inquire" onSubmit="return false;">
Search or inquire here: <input type="text" id="search" name="search" value="<?php search($_GET['search']) ?>" autocomplete="off"/>
<input type="button" name="btnSearch" id="btnSearch" onClick="formula();"/>
</form>
</div>
我想要在这里发生的是,当点击 btnSearch 它以查询形式从 search 中激活 formula(),然后滚动到另一个div中的值, EM>公式。如果我将 document.getElementById(this.form.search.value).scrollIntoView(); 直接放入 btnSearch 的 onClick 中,然而,无论我如何将其设置为函数,它都拒绝工作。有人在这里知道为什么它可能会这样做,或者我可以修复它吗?
What I want to happen here is that, when btnSearch is clicked, it activates formula(), taking the value from search in form inquire, and then scrolling to the value in a separate div I have called formula. This works if I put document.getElementById(this.form.search.value).scrollIntoView(); directly into the onClick for btnSearch, however it refuses to work no matter how I set it up as a function. Does anyone here know why it might be doing this, or how I can fix it?
推荐答案
它不工作,因为你的功能是错误的:
It's not working because your function is erroneous:
function formula() {
document.getElementById(document.forms.inquire.search.value).scrollIntoView();
}
该代码获取搜索字段的值并将其用于id 抬头。这可能是一个好主意,检查实际上是否存在输入值为id的值:
That code gets the value of the search field and uses it for an "id" lookup. It'd probably be a good idea to check that there actually is an "id" with the value typed in:
function formula() {
var element = document.getElementById(document.forms.inquire.search.value);
if (element) element.scrollIntoView();
}
这篇关于Javascript功能不会激活onClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!