【问题标题】:jQuery: Submit function for two identical forms on same pagejQuery:在同一页面上提交两个相同表单的功能
【发布时间】:2018-02-15 17:27:02
【问题描述】:

我有一个页面,它在不同部分包含两次相同的表单——一个简单的单一输入字段,用于输入电子邮件地址。我配置了以下submit 函数,我想知道允许两种形式彼此独立运行的最简单方法;因为正如我现在配置的那样,这两种形式都被脚本有效地视为一个实体。有没有办法指定在.form div 中提交 nearestactive 表单?或者我应该以完全不同的方式配置提交功能?感谢您在这里提供任何见解。

$(document).ready(function() {
var infoForm = $('.form form');
var formSuccessMsg = $('p.success');
var formErrorMsg = $('p.error');

infoForm.submit(function() {
    // var url = $(this).attr('action');
    var url = $(this).attr('action') + '?showtemplate=false';
    var emailInput = $('.form input[type=text]').val();
    var data = $(this).serialize();

    if( emailInput.length === 0 || emailInput == '' || !isValidEmailAddress(emailInput) ) {
        formErrorMsg.show();
        return false;
    } else {
        $.post(url, data, function(data, textStatus, jqXHR) {
            // alert('post came back');
            infoForm.hide();
            formErrorMsg.hide();
            formSuccessMsg.show();
            console.log(emailInput);
        });
        return false;
    }
});
});

更新:这是标记(表单代码由 CMS 生成):

<div class="form">
   <div class="form-response">
      <p class="success">Thank you!</p>
      <p class="error">Please enter a valid email address</p>
   </div>
   <form id="mc2e7cmoduleform_1" method="post" action="post" enctype="multipart/form-data">
      <div class="info-packet-form">
         <div class="required"><input type="text" name="mc2e7cfbrp__35" value="" size="25" maxlength="80"   id="fbrp__35" /></div>
         <div class="submit"><input name="mc2e7cfbrp_submit" id="mc2e7cfbrp_submit" value="Submit" type="submit" class="fbsubmit"   /></div>
      </div>
   </form>
</div>

【问题讨论】:

  • 此外,您还应该分享您的 HTML。很难猜出你的代码结构...
  • var emailInput = $(this).find('input[type=text]').val();
  • 以上将起作用。它将从$(this) 表单(不是另一个)中找到您的输入值。或者$('input[type=text]', $(this)).val() 也应该可以工作。
  • var emailInput = $(this).find('input[type=text]').val(); 而不是var emailInput = $('.form input[type=text]').val(); 将选择当前提交表单中的输入。如果您想选择隐藏此特定表单,请在提交回调中选择当前表单var currentInfoForm = $(this);,然后再调用$.post。然后在您的 $.post 回调中执行 currentInfoForm.hide()

标签: javascript jquery forms form-submit


【解决方案1】:
$(document).ready(function() {
    var infoForm = $('.form form');

    infoForm.submit(function() {
        // create a var to reference this specific form
        var $thisForm = $(this);

        // assuming you want to only show the messages nearest to your form, 
        // you can select the parent div and find the response divs in there too.
        // create a reference to the specific parent div (div class="form") 
        var $parent = $thisForm.parent();

        // these will reference the specific fields specific to this form
        var formSuccessMsg = $parent.find('p.success');
        var formErrorMsg = $parent.find('p.error');

        var url = $thisForm.attr('action') + '?showtemplate=false';
        var emailInput = $thisForm.find('input[type=text]').val();

        if( emailInput.length === 0 || emailInput == '' || !isValidEmailAddress(emailInput) ) {
            formErrorMsg.show();
            return false;
        } else {
            $.post(url, $thisForm.serialize(), function(data, textStatus, jqXHR) {
                // alert('post came back');
                $thisForm.hide();
                formErrorMsg.hide();
                formSuccessMsg.show();
                console.log(emailInput);
            });
            return false;
        }
    });
});

【讨论】:

  • 很高兴它成功了。我知道一页上两种形式的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
  • 2012-11-29
  • 2017-03-21
  • 1970-01-01
  • 2015-07-13
  • 1970-01-01
  • 2015-05-13
相关资源
最近更新 更多