jquery - 在提交表单之前,如何使用 jquery 将表单字段添加到表单中
问题描述
在我的页面上,我有两个按钮。
- 无需注册即可购买
- 进行购买并将我添加到邮件列表中。
两个按钮都提交相同的表单,但是我需要其中一个按钮在表单提交之前将隐藏的表单字段添加到表单中,以允许在生成的 PHP 中使用 POST 变量。
我无法让以下工作。TIA
jQuery(document).ready(function(){
jQuery('.place-order-with-newsletter').on('click', function(){
jQuery('form#dc-checkout-form').submit(function(){
jQuery('<input />').attr('type', 'hidden')
.attr('name', 'dc-opt-in')
.attr('value', '1')
.appendTo('form#dc-checkout-form')
return true;
})
})
})
解决方案
将按钮移到表单外或将类型更改为“按钮”
<button type="button"></button>
默认情况下,表单上的按钮使提交。
并将所有代码移动到单击事件并最终提交表单
jQuery(document).ready(function(){
jQuery('.place-order-with-newsletter').on('click', function(){
jQuery('<input />').attr('type', 'hidden')
.attr('name', 'dc-opt-in')
.attr('value', '1')
.appendTo('form#dc-checkout-form')
jQuery('form#dc-checkout-form').submit()
})
})
推荐阅读
- javascript - 从父组件清除子组件状态
- mysql - 在两个事件发生之间聚合数据
- java - DDD:聚合和实体的关系
- r - R中的GGPlot2条形图中未生成条形图
- c - 在 c 中使用 memcpy 后,我尝试打印整数数组
- r - 如何生成具有以编程方式设置的参数值的 .rmd 文件
- facebook - V11.0 Facebook Graph API - 自/直到不填充整个日期范围
- python - 并非所有参数都在 SQL 语句 Python - MySql 中使用
- php - 如何使用 PHP cURL 和 XML 解析数据
- git - git:撤消对文件/子目录的修改,无论其状态如何