Div类中的输入被分组为JSON数组

时间:2016-09-06 01:20:30

标签: javascript jquery html json

JS小提琴here

整体情况是,我正在尝试使用jquery-steps完成发送多步骤表单。第1部分非常简单,但在第2部分中,用户可以选择收件人或克隆字段,以便他们可以添加更多字段。

enter image description here

我粘贴的小提琴不包括克隆功能,但每个克隆字段的名称属性始终相同。 (第1个rid和recipient_id,第2个rid和recipient_id,依此类推)。 我试图避免在PHP中的每一个循环,所以我将它们全部放在一个json.stringify变量中,我可以在提交时传递。

[{"rid":"1","recipient_id":"2","email_type":"to","to_cc_bcc":"","start_dte":"","end_dte":""},
{"rid":"1","recipient_id":"3","email_type":"Body","to_cc_bcc":"","start_dte":"","end_dte":""}]

我的小提示显示'未捕获的ReferenceError:未定义标题'。我如何才使其仅适用于特定div类中的字段?

HTML:

<div id="testform">
    <input class="input_rid" type="text" name="rid" id="rid" value="" placeholder="rid" required>
    <input class="input_recipient_id" type="text" name="recipient_id" id="recipient_id" value="" placeholder="recipient_id" required>
    <select class="select_eml" name="email_type" id="email_type" required>
        <option value="">Email type</option>
        <option value="Body">Body</option>
        <option value="Body & Attachment">Body & Attachment</option>
        <option value="Link">Link</option>
        <option value="Internal Attachment">Internal Attachment</option>
        <option value="External Attachment">External Attachment</option>
    </select>
    <select class="select_tcb" name="to_cc_bcc" id="to_cc_bcc">
        <option value="">To_CC_BCC</option>
        <option value="to">To</option>
        <option value="cc">CC</option>
        <option value="bcc">BCC</option>
    </select>
    <input class="input_sd" type="date" name="start_dte" id="start_dte" value="" placeholder="start_dte" required>

    <input class="input_ed" type="date" name="end_dte" id="end_dte" value="" placeholder="end_dte">
    <input type= "button" onclick="test()" value="Save">
</div>

JS:

$.fn.serializeObject = function()
{
   var o = {};
   var a = this.serializeArray();
   $.each(a, function() {
       if (o[this.name]) {
           if (!o[this.name].push) {
               o[this.name] = [o[this.name]];
           }
           o[this.name].push(this.value || '');
       } else {
           o[this.name] = this.value || '';
       }
   });
   return o;
};

window.test = function() {
headers
var formData = JSON.stringify(jQuery('#testform').serializeObject());
console.log(formData);
}

更新:小提琴已更新,包含克隆的东西。

0 个答案:

没有答案