简体   繁体   English

SerializeObject和SerializeArray没有显示在console.log上

[英]SerializeObject and SerializeArray doesn't show on console.log

My JSFiddle here 我的JSFiddle 在这里

I know there has been loads of posts about this here in SO but I'm wondering why these (SerializeObject/Serialize/SerializeArray()) doesn't show on my console.log after onclick. 我知道在SO中有很多与此相关的帖子,但我想知道为什么onclick之后这些内容(SerializeObject / Serialize / SerializeArray())不在我的console.log中显示。

HTML: HTML:

<form action="#" method="post" id="testform" name="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="" selected="selected" disabled="disabled">
                        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="" selected="selected" disabled="disabled">
                        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 class="button small radius right inline" type="button" onclick="return test();" value="Save to Console Log">

</form>

JS: 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;
};


function test() {
var v = $("#testform").serializeObject();
console.log(v);
console.log($("#testform").serialize());
console.log($("#testform").serializeArray());
}

The problem here is that the test function is declared inside a window load scope and when you click on that button the interpreter can't find the invoked function. 这里的问题是, 测试函数在窗口加载范围内声明,并且当您单击该按钮时,解释器找不到调用的函数。

To get that example working, move the function declaration outside the $(window).on('load') scope or just declare it globally: 为了使该示例正常工作,请将函数声明移至$(window).on('load')范围之外,或仅在全局范围内进行声明:

window.test = function() {
 var v = $("#testform").serializeObject();
 console.log(v);
 console.log($("#testform").serialize());
 console.log($("#testform").serializeArray());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM