我有这个表格:
<form id="segform" action="TestServlet" method="POST" >
<div id="actives" class="rounded-corners">
<div class="column">
<div class="portlet"><a href="#">link1</a></div>
</div>
</div>
<div id="inactives" class="rounded-corners">
<div class="column">
<div class="portlet"><a href="#">link2</a></div>
</div>
</div>
<input type="hidden" name="ffactives" value="foovalue" />
<input type="hidden" name="ffinactives" value="foovalue" />
<input type="submit" />
</form>
我需要在提交表单时将链接的内容设置为输入:
ffactives =“link1”而不是“foovalue” ffinactives =“link2”而不是“foovalue”
我现在使用jquery代码:
$('#segform').submit(function(){
var act = $('#actives div.column div.portlet a').text();
var inact = $('#inactives div.column div.portlet a').text();
$('input[name=ffactives]').val(act);
$('input[name=ffinactives]').val(inact);
return true;
});
但是当我收到输入时,值没有改变。 我方面 ffactives = "link1"和 ffinactives = "link2",但我收到 ffactives = "foovalue"和 ffinactives = "foovalue"
怎么了
最佳答案
你所拥有的从表面上看是有效的,you can test it here ,需要检查的一些事项:
- 这是在
document.ready
中运行吗?处理程序?例如您的代码应该包含在
中$(function() { /* current code */ });
这样$('#segform')
直到<form>
才运行位于 DOM 中并准备好被发现(否则它会 Hooksubmit
处理程序最多 0 个元素。 - 您遇到任何 JavaScript 错误吗?如果在代码运行之前发生任何情况,
submit
处理程序不会被绑定(bind),并且您将获得默认的提交行为...这就是您所看到的。
关于Jquery 提交时不要在输入上设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4093817/