2012-04-23 81 views
1

初学者问题: 我想将window.location.hash传递给我的表单操作参数。我怎样才能做到这一点的JavaScript调用?Javascript将字符串返回到Html

我当前的非工作代码为:

<form method="POST" 
    action="j_security_check"+window.location.hash> 
+0

使用的onload:http://www.w3schools.com/jsref/event_body_onload.asp – ControlAltDel 2012-04-23 18:11:39

+0

[如何使用javascript添加/更新属性到HTML元素?](http://stackoverflow.com/questions/710275/how-to-add-update-an-attribute-to-an-html -element-using-javascript) – 2012-04-23 18:13:02

回答

3

您的JavaScript必须基于某种事件。所以,你可能想这样做,当你的提交按钮被按下或东西:

<form method="POST" action=""> 
<input type="submit" value="Submit" onclick="this.form.action = 'j_security_check'+window.location.hash" /> 

..... 
+0

谢谢!这样可行。 – sworded 2012-04-23 18:20:41

+0

-1 javascript不需要基于任何要执行的事件。 – iambriansreed 2012-04-23 21:01:17

0
<form name="hello" method="POST" action=""> 
    Hello 
</form>​ 

<script> 
    document.forms.hello.action = 'j_security_check'+window.location.hash; 
    alert(document.forms.hello.action); 
</script> 

证明:http://jsfiddle.net/2QE6h/1