可以在JQuery中更改名称/序列化​​输出吗? [英] Possible to change names/serialization output in JQuery?

查看:84
本文介绍了可以在JQuery中更改名称/序列化​​输出吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个要通过JQuery/Ajax提交的表单 HTML:

So, I have a form that I'm submitting via JQuery/Ajax HTML:

<form id="emailsignup" method="post">
<p><input name="contactfirst_name" placeholder="Your First Name" type="text"> 
<input name="contactemail" placeholder="youremail@address.com" type="text"> 
<button class="btn btn-success btn-large">Sign Me Up</button>
<input name="action" value="ext_addcontact" type="hidden">
<input name="selectedgroupname" value="Website Opt-Ins" type="hidden">
<input name="groupownerid" value="[p:pid]" type="hidden">
<input name="redirect" value="path/to/success.html" type="hidden"> 
<input name="redirect_error" value="path/to/failure.html" type="hidden">

然后通过Javascript将其传递 JAVASCRIPT

And then passing it through via Javascript JAVASCRIPT

<script>
$(document).ready(function(){
$("button").click(function(){
$.post("path/to/index.php", $(".emailsignup").serialize());
});
});
</script>

好的,所以这里很有趣.我需要更改序列化数据格式的方式,因为服务器正在寻找此输出:

OK, so here's where it gets fun. I need to change the way the format in which the data is serialized, because the server is looking for this output:

contact%5Bfirst_name%5D = data& contact%5Bemail%5D = data& etc = data

服务器正在联系后寻找要放在方括号中的单词 (即:我需要找到一种在序列化时输入方括号或等价的字符代码的方法,以将contactfirst_name转换为contact [first_name]并将contactemail转换为contact [email])-但是,我不能仅重命名contact [ first_name]和contact [email].这是在营销自动化平台上托管的一种形式,[first_name]和[email]是系统变量,这意味着它们将在页面加载时进行转换(为什么对我不知道的两个对象使用相同的语法……我).

The server is looking for the words after contact to be in square brackets (IE: I need to find a way, when serializing, to input the square brackets or their character code equivalents to turn contactfirst_name into contact[first_name] and contactemail into contact[email]) - however, I cannot just rename the names contact[first_name] and contact[email]. This is a form hosted on a marketing automation platform, and [first_name] and [email] are system variables, which means that they would get converted on page load (why use the same syntax for both I don't know... beyond me).

所以,我想我想在序列化后使用Javascript插入方括号,但实际上并没有拼出全名.

So, I guess I'm trying to use Javascript to insert the square brackets in after serializing, but without ever actually spelling out the full name.

在表单上更改名称是不可行的,因为在托管表单的平台上使用未编码的字符(即contact [first_name],contact [email])会触发变量.出于同样的原因,更改为我发布到的php文件不是一个选择,因为它是一个封闭的平台.

例如,提交前有什么办法吗?如果是这样,我应该在哪里将其插入脚本中,并传递什么参数?

Is there any way to do this with, say, beforesubmit? If so, where do I insert it into my script and what arguments am I passing through?

就舒适度而言,我将在W3Schools和另一个网站上找到的示例中将此脚本拼凑在一起-除了替换有占位符的class/id之外,我通常是Jquery和Javascript的新手.

In terms of comfort level here, I cobbled this script together from an example I found on W3Schools and another website - I'm very much a novice at Jquery and Javascript in general aside from replacing classes/id's where there are placeholders.

推荐答案

所以我觉得自己是个白痴-问题是数据是以序列化方式发布的.通过为每个字段设置ID,然后通过.setAttribute重命名名称,我可以实现所需的功能.我敢肯定这是超大体积的,可能会更简单/优雅,但是,我正在学习:

So I feel like an idiot - the issue was that the data was being posted in a serialize fashion. I was able to achieve what I needed by setting ID's to each field and then renaming the names through .setAttribute. I'm sure this is super bulky and could be much more simple/elegant, but hey, I'm learning:

function quoteRequest() {
var fName = document.getElementById("fname");
var lName = document.getElementById("lname");
var contactEmail = document.getElementById("email");
var productQty = document.getElementById("qty");
var additionalInfo = document.getElementById("additionalinfo");
var product = document.getElementById("product");
var productnum = document.getElementById("productnum");
fName.setAttribute('name', 'contact\[first_name\]');
lName.setAttribute('name', 'contact\[last_name\]');
contactEmail.setAttribute('name', 'contact\[email\]');
productQty.setAttribute('name', 'contact\[qty\]');
product.setAttribute('name', 'contact\[product\]');
productnum.setAttribute('name', 'contact\[productnum\]');
document.getElementById("quoteform").submit();
}

这篇关于可以在JQuery中更改名称/序列化​​输出吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆