简体   繁体   English

将表单名称发送到JS / jQuery并序列化

[英]Send Form Name to JS/jQuery & Serialize

It works when inputs are empty but as soon as they have a value it returns nothing. 当输入为空时有效,但是一旦输入一个值,它什么也不返回。

UPDATE: It's if I send the form without entering any inputs, then enter the inputs and send again, that nothing is returned. 更新:如果我不输入任何输入就发送表单,然后输入输入并再次发送,则不会返回任何内容。

NEW UPDATE: I hadn't included the function I created to handle errors. 新更新:我没有包括我创建的用于处理错误的功能。 That function appears to be causing the problem somehow. 该功能似乎以某种方式引起了问题。 I'm adding the code below. 我在下面添加代码。 When the function is removed from the code, the error does not occur. 从代码中删除该功能时,不会发生此错误。

Error Function 错误功能

function showError(id){
        $('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
        $("#"+id).css( "background-color", "#ffcece" );
        $("#sendBtn").attr("onclick","sendForm()");
        $('#sendBtn').css("background-color", "#29559c");
        document.getElementById('sendBtn').innerHTML = "Submit Application";
        $('#frm-err').css("color", "red");
        $('#frm-err').html('Please fill out the entire application');
}

HTML 的HTML

<form name="membershipForm" id="membershipForm" onsubmit="return false">
    <input required type="text" id="Name" name="Name" placeholder="Name">
    <input required type="text" id="Pin" name="Pin" placeholder="Pin">
    <button id="sendBtn" onclick="sendForm(form.name);return false;">Submit</button>
</form>

JS/jQuery JS / jQuery

function sendForm(formName) {       
    var formData = $("#"+formName).serialize().replace(/\+/g,'%20');
    alert(formData);
}

在处理错误的函数中,我重置了onclick,但未包含(form.name)。

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

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