【发布时间】:2013-11-14 19:27:15
【问题描述】:
我有这个代码
<script>
function cA( url )
{
document.myform.action = url;
}
</script>
<input type="whatever" name="whatever" id="whatever" />
<button class="whatever-button" id="defaultAction" value="Go for it" name="whatever" type="submit" onClick="cA('whatever.php#anchor')" >Go for it
</button>
当您单击 Go for it 按钮时,正在加载 whatever.php#anchor。我需要如何更改输入标签的元素才能执行与单击按钮相同的cA('whatever.php#anchor')?
因此,当用户在字段中按下回车键时,应该加载站点 whatever.php#anchor,或者更具体地说,执行 cA('whatever.php#anchor')。
页面有更多按钮,因此将一个按钮设为默认按钮不起作用。
PS:
将所有内容包装在一种形式中不起作用,因为页面的结构是
<form>
<script>
function cA( url )
{
document.myform.action = url;
}
</script>
<input type="whatever1" name="whatever1" id="whatever1" />
<button class="whatever1-button" id="defaultAction1" value="Go for it" name="whatever1" type="submit" onClick="cA('whatever.php#anchor1')" >Go for it
</button>
<input type="whatever2" name="whatever2" id="whatever2" />
<button class="whatever2-button2" id="defaultAction2" value="Go for it" name="whatever2" type="submit" onClick="cA('whatever.php#anchor2')" >Go for it
</button>
<input type="whatever3" name="whatever3" id="whatever3" />
<button class="whatever3-button" id="defaultAction3" value="Go for it" name="whatever3" type="submit" onClick="cA('whatever.php#anchor3')" >Go for it
</button>
...
</form>
【问题讨论】:
-
将其转换为
<form>并使用onsubmit事件 -
@Brewal 我更新了。
标签: javascript html