简体   繁体   English

如果表单通过验证,则删除“提交按钮”

[英]Remove Submit Button if the form is validated

I am using Magento and created a custom form, and basically what I want to do is prevent a submit button to be click more than once (whether that is a double click, or if the user just gets impatient and clicks the button again after a few seconds). 我正在使用Magento并创建了一个自定义表单,基本上我想做的是防止多次单击“提交”按钮(无论是双击还是用户不耐烦并在单击后再次单击按钮)几秒钟)。

The form is using the Magento Javascript validation method to validate the fields and IF the fields are all validated then what I would like to do is to remove the submit button on the first click and replace it with a "In process..." message. 表单使用Magento Javascript验证方法来验证字段,并且如果所有字段都已验证,那么我想做的就是删除第一次单击时的“提交”按钮,并将其替换为“处理中...”消息。 This way there is no way that a user can double click or multiple click the button. 这样,用户无法双击或多次单击按钮。

If the fields are not all validated then move the submit button down and just above it display a message that may read "Please fill out all required fields and submit form again". 如果未对所有字段都进行验证,则向下移动“提交”按钮,并在其上方显示一条消息,内容为“请填写所有必填字段,然后再次提交表单”。

Below is the form with just validation, but I would really like to know how to apply the what I mentioned above. 下面是带有验证的表格,但我真的很想知道如何应用我上面提到的内容。

Any help would be SO appreciated!!! 任何帮助将不胜感激!!! Thanks in advance. 提前致谢。

<form name="<em><strong>my-custom-form</strong>" id="my-custom-form" action="" method="post">

<label for="firstname">< ?php echo $this->__('First name') ?> <span class="required">*</span></label><br />
<input id="firstname" name="firstname" class="<em/><strong>input-text required-entry</strong>" />

<label for="lastname">< ?php echo $this->__('Last name') ?> <span class="required">*</span></label><br />
<input id="lastname" name="lastname" class="<em/><strong>input-text required-entry</strong>" />

<label for="useremail">< ?php echo $this->__('Email') ?> <span class="required">*</span></label><br />
<input type="text" name="useremail" id="useremail" class="<em/><strong>input-text required-entry validate-email</strong>" />

<input type="submit" name="submit" value="<?php echo $this-/>__('Submit') ?>" />

</form>< ?php /* END OF my-custom-form */?>

<script type="text/javascript">
 //< ![CDATA[
   var customForm = new VarienForm('<em><strong>my-custom-form</strong>');
 //]]>
</script>

Not sure how Magento might fit in because I'm not overly familiar with it, but the process typically works like this: 不知道Magento可能如何适应,因为我不太熟悉它,但是该过程通常是这样的:

$('#my-custom-form').submit(function(){
    $('input[type=submit]', this).attr('disabled', true);

    // validate form

    if (valid) {
        return true;
    } else {
        $('input[type=submit]', this).attr('disabled', false);
        return false;
    }
});

You could look into UI blocking as one possible solution. 您可以将UI阻止作为一种可能的解决方案。 I use this one. 我用这个。

If you're not using jQuery already, you could try just hiding the submit button, rather than removing it completely. 如果您尚未使用jQuery,则可以尝试仅隐藏“提交”按钮,而不是完全删除它。 Removing the button can cause issues with event binding, depending on how you set them up. 删除按钮可能会导致事件绑定问题,具体取决于您如何设置它们。

This is probably the most help you're going to get without a little more code/information. 如果没有更多的代码/信息,这可能是您将获得的最大帮助。

I don't see your validate code above, but if we assume that it's a boolean function you can just: 我没有在上面看到您的验证代码,但是如果我们假设它是布尔函数,则可以:

if (validatation()) {
  $('#my-custom-form').submit(function(){
   $('input[type=submit]', this).attr('enabled', 'enabled');
   $('input[type=submit]', this).val('In process...');
  });
} else { 
  $('#my-custom-form').submit(function(){
    $('input[type=submit]', this).attr('disabled', 'disabled');
  });
}

I would suggest two things: 我建议两件事:

1) Drop the "In process..." thing, it just makes things harder for the user the 2nd time unless you keep doing validation() on every change and setting the text back. 1)删除“ In process ...”(正在处理中...)的东西,这只会使用户第二次变得更困难,除非您对每次更改都继续执行validation()并重新设置文本。

2) Add a double-click prevention: 2)添加双击预防措施:

    $('my-custom-form').submit(function(){
       $('input[type=submit]', this).attr('disabled', 'disabled');
       $('select', this).attr('disabled', 'disabled');
       $('input[type=text]', this).attr('readonly', 'readonly');
       $('textarea', this).attr('readonly', 'readonly');
  });

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

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