我想在使用JQuery提交表单之前将表单字段添加到表单中。 我已经尝试了以下stackoverflow问题,但它们没有工作:
如何在提交前添加其他字段?
jQuery - 在提交时添加其他参数(NOT ajax)
如果我没有向表单添加任何参数,则提交有效:
$('.submit_button').click(function(){ $('#quote_form').submit(); });上面的代码有效。 但是,一旦我尝试添加字段,表单就不会被提交。 它没有到达服务器。 这是我正在使用的代码:
$('.submit_button').click(function(){ console.log('1'); $("#quote_form").submit( function(eventObj) { console.log('2'); $(this).append('<input type="hidden" name="dog" value="rover" /> '); return true; }); });第一个console.log出现在控制台中,但第二个不出现。 如何使此代码生效?
I am trying to add form fields to a form before submitting it using JQuery. I've already tried the following stackoverflow questions and they have not worked:
How to add additional fields to form before submit?
jQuery - add additional parameters on submit (NOT ajax)
If I don't add any parameters to the form, the submit works:
$('.submit_button').click(function(){ $('#quote_form').submit(); });The code above works. Once I try to add a field, however, the form does not get submitted. It is not reaching the server. Here is the code I am using:
$('.submit_button').click(function(){ console.log('1'); $("#quote_form").submit( function(eventObj) { console.log('2'); $(this).append('<input type="hidden" name="dog" value="rover" /> '); return true; }); });The first console.log appears in the console but the second does not. How do I get this code to work?
最满意答案
你没有提交表格。 尝试这个:
$("#quote_form").submit( function(eventObj) { $(this).append('<input type="hidden" name="dog" value="rover" /> '); return true; }); $('.submit_button').click(function(){ $("#quote_form").submit(); });$(..).submit(handler)不提交但仅为submit事件注册处理函数。 你需要调用$(..).submit()而不用参数来提交。
You're not submitting the form. Try this:
$("#quote_form").submit( function(eventObj) { $(this).append('<input type="hidden" name="dog" value="rover" /> '); return true; }); $('.submit_button').click(function(){ $("#quote_form").submit(); });$(..).submit(handler) does not submit but only register a handler function for the submit event. You need to call $(..).submit() without parameters for it to submit.
更多推荐
提交,submit,电脑培训,计算机培训,IT培训"/> <meta name="description" c
发布评论