使用href调用javascript函数

时间:2011-07-02 13:40:41

标签: javascript

我正在开发一个网站,但在javascript方面我不是专家。我正在使用我在互联网上找到的脚本来搜索页面中的文本字符串。要调用此函数,请使用以下代码:

  <div class="pesquisa" id="pesquisa"><form name="f1" action=""
  onSubmit="if(this.t1.value!=null && this.t1.value!='')
  findString(this.t1.value);return false">
  <input name="t1" type="text" /></form>
  </div>

这很有效,当我输入文字并点击回车时,他会在页面上找到它。 现在我旁边有一个“按钮”,表示为:

<a href='javaScript:document.f1.findString(this.t1.value)'>
<div class="enviar" id="enviar">Pesquisar</div></a></div>

我希望这个“按钮”使用表单中插入的值调用相同的函数。但这不是这样的。 关于如何做到这一点的任何想法?

提前致谢,Cláudio

2 个答案:

答案 0 :(得分:1)

首先,在文本框中添加id属性:

<input id="t1" name="t1" type="text" />

然后,在<a>标记中,您可以执行此操作:

<a href="#" onclick="findString(document.getElementById('t1').value); return false;">Click Me</a>

答案 1 :(得分:0)

试试这个..将一个id属性添加到t1(也称为“t1”),然后像这样引用它:

<a onclick="javaScript:findString(document.getElementById('t1').value)">