简体   繁体   English

jQuery警报两次消息为什么?

[英]jquery alert twice the message why?

Existing code is below. 现有代码如下。

The issue is that when the data is ok, the alert "You have sucessfully subscribed to the alert" is followed by "There was a problem with the alert" and when the data is wrong the "There was a problem with the alert" its alerted twice. 问题是,当数据正常时,“您已成功订阅了警报”警报之后是“警报存在问题”,当数据错误时,“警报存在问题”两次提醒。 Why is this? 为什么是这样?

jQuery(document).ready(function(){
  var alertForm = jQuery( "#sub_alert" );
  // Validation (Alert)
  alertForm.validate({
    rules: {
      alert_email: {
        required: true,
        email: true
      }
    },
    messages: {
      alert_email: {
        required: "Email is required",
        email: "Enter valid email"
      }
    }
  });
  jQuery(".sub_button").click(function(){
    if( alertForm.valid() ) {
      jQuery.post('<?php echo osc_base_url( true ); ?>', {email:jQuery("#alert_email").val(), userid:jQuery("#alert_userId").val(), alert:jQuery("#alert").val(), page:"ajax", action:"alerts"},
        function(data) {
          if(data == 1) { alert('<?php echo osc_esc_js( __( 'You have sucessfully subscribed to the alert', 'test' ) ); ?>'); }
          else if(data == -1) { alert('<?php echo osc_esc_js( __( 'Invalid email address', 'test' ) ); ?>'); }
          else { alert('<?php echo osc_esc_js( __( 'There was a problem with the alert', 'test' ) ); ?>'); 
        };
      });
      return false;
    }
  });
});

Your submit button has a class selector. 您的提交按钮具有一个类选择器。 Do you maybe have multiple posts? 您可能有多个帖子吗? If you run jQuery(".sub_button").length , do you get 2? 如果运行jQuery(".sub_button").length ,得到2吗? This is admittedly a guess since I can't see the entire html layout. 由于我看不到整个html布局,因此这是一个猜测。

jQuery(".sub_button").click(function(){
  if( alertForm.valid() ) {
    jQuery.post('<?php echo osc_base_url( true ); ?>', {email:jQuery("#alert_email").val(), userid:jQuery("#alert_userId").val(), alert:jQuery("#alert").val(), page:"ajax", action:"alerts"},
      function(data){
        if (data == 1) { alert('<?php echo osc_esc_js( __( 'You have sucessfully subscribed to the alert', 'test' ) ); ?>'); }
        else if (data == -1) { alert('<?php echo osc_esc_js( __( 'Invalid email address', 'test' ) ); ?>'); }
        else { alert('<?php echo osc_esc_js( __( 'There was a problem with the alert', 'test' ) ); ?>'); 
      }
    });
    return false;
  }
});

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

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