javascript Jquery.submit() - 提交前更改表单值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8280795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery.submit() - Changing Form Values before submit
提问by thoughtpunch
I have some javascript that submits to an HTML form in my app upon a "click" event like so....
我有一些 javascript 在“点击”事件时提交到我的应用程序中的 HTML 表单,就像这样......
google.maps.event.addListener(marker, 'click', function() {
$('form').submit()
});
However I want to change the data before I send it. Is this possible? I don't want to use Ajax $.post if I can help it. I just want to change what the form sends to my Rails controller before submit.
但是我想在发送之前更改数据。这可能吗?如果可以的话,我不想使用 Ajax $.post。我只想在提交之前更改表单发送到我的 Rails 控制器的内容。
Thanks!
谢谢!
回答by Esailija
If you already have a field with name="search" you can change it's value like so:
如果您已经有一个 name="search" 的字段,您可以像这样更改它的值:
google.maps.event.addListener(marker, 'click', function() {
$('form input[name="search"]').val( place.name );
$('form').submit()
});
Or if not:
或者,如果不是:
google.maps.event.addListener(marker, 'click', function() {
$('form').append('<input type="hidden" name="search" value="'+place.name+'" />').submit()
});
Assuming place.name
is known like in your previous question.
假设place.name
在您之前的问题中是已知的。
回答by Gregg B
You can update the value of specific fields by id before you call submit:
您可以在调用 submit 之前通过 id 更新特定字段的值:
$("form #fieldID").val("Value you want to change");