使用JQuery submit()向表单添加字段(Adding fields to form with JQuery submit())

编程入门 行业动态 更新时间:2024-10-27 04:25:34
使用JQuery submit()向表单添加字段(Adding fields to form with JQuery submit())

我想在使用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

本文发布于:2023-08-06 03:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1443595.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   表单   submit   JQuery   fields

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!