jQuery:如何在提交前修改表单字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5910128/
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: How to modify form field before submit
提问by GabiMe
But without the ugly effect that the new value is displayed in the the form.
但是没有新值显示在表单中的丑陋效果。
if i do:
如果我做:
$('#submit_button').click(function(){
$('#field1').val('newval');
$('#form1').submit(); });
The #field1 will display (for a split second) the new value which is kind of ugly..
#field1 将显示(一瞬间)新值,这有点难看。
回答by Rudi Visser
From the code above, it looks like you're completely ignoring the value of the visible form element, if so why not just put a hidden form field with the name you're using, and ignore what's posted through from the text field.
从上面的代码来看,您似乎完全忽略了可见表单元素的值,如果是这样,为什么不使用您正在使用的名称放置一个隐藏的表单字段,而忽略从文本字段中发布的内容。
Instead, if that code wasn't real and you're planning on modifying the value instead, you could use the same technique, something like this:
相反,如果该代码不是真实的并且您打算改为修改该值,则可以使用相同的技术,如下所示:
<input type="hidden" name="field1" id="field1" value="" />
<input type="text" name="field1_real" id="field1_real" value="" />
jQuery:
jQuery:
$('#submit_button').click(function() {
var newValue = $('#field1_real').val(); // Modify me
$('#field1').val(newValue);
$('#form1').submit();
});
回答by Nicolas Le Thierry d'Ennequin
Why don't you use a hidden field?
为什么不使用隐藏字段?
<input type="hidden">
回答by fearofawhackplanet
Use a hidden field in the form to submit newval
along with the original value?
使用表单中的隐藏字段newval
与原始值一起提交?
I'm not really sure why you would try and do what you are doing.
我不确定你为什么要尝试做你正在做的事情。