如果是这种情况则停止执行

时间:2017-05-19 13:21:58

标签: javascript

$('.btn-process-request', node).bind('click', function(e){
  e.preventDefault();
  var data = {};
  var fax_number_empty = {{ contract.request.customer.fax }}
  if (fax_number_empty == 0) {
    alert('Please, attach the fax number to your profile');
  }
  alert('Hello! What is your name?');
  if ($(this).data('reason')) {
      data.reason = prompt($(this).data('reason'));
      if (!data.reason.length && $(this).data('reason-required')) {
        alert($(this).data('reason-required'));
        return false;
      }
  }
  $.ajax({
    url : $(this).attr('href'),
    type: 'POST',
    data : data,
    success: function(data, textStatus, jqXHR) {
      if (data.success) {
          if (data.redirect_to) {
            window.location.href = data.redirect_to;
          }
          else if (data.reload) {
            window.location.reload();
          }
      }
      else {
        alert('Error! See console for details :(');
        console.error(textStatus, data);
      }
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.error(textStatus, errorThrown);
    }
  });
  return false;
});

在这段代码中,我创建了行

var fax_number_empty = {{ contract.request.customer.fax }}
      if (fax_number_empty == 0) {
        alert('Please, attach the fax number to your profile');
      }

我不知道如何正确实现这一点。我想说,好吧,如果contract.request.customer.fax为空,则会显示一条警告,上面写着“请将传真号码附加到您的个人资料中”。这里最重要的是停止执行。换句话说,这些线路不会被执行。任何人都有时间告诉我如何改进该代码吗?

P.S。如果我的问题不清楚,请不要羞于告诉我。

1 个答案:

答案 0 :(得分:1)

如果字段fax_number_empty falsy value ,则返回,并且函数将跳到最后,避免代码的其余部分。

   if (!fax_number_empty) {
     alert('Please, attach the fax number to your profile');
     return; 
   }
相关问题