简体   繁体   English

jQuery表单验证和变更提交

[英]jquery form validation, and submit-on-change

I want to make all my settings forms across my site confirm that changes are saved, kinda like facebook does if you make changes in a form and then try to navigate away without saving. 我想让我在网站上的所有设置表单都确认更改已保存,就像Facebook在您对表单进行更改然后尝试导航而不保存时一样。 So I'm disabling the submit button on the forms only enabling if the values change. 因此,我仅在值更改时禁用表单上的提交按钮。 I then prompt the user to hit save before they leave the page in the case that they do have changes pending. 然后,如果他们确实有待处理的更改,我会提示用户在离开页面之前点击保存。

var form = $('form.edit');
if(form.length > 0) {
  var orig_str = form.serialize();
  $(':submit',form).attr('disabled','disabled');
  form.on('change keyup', function(){
    if(form.serialize() == orig_str) {
      setConfirmUnload(false);
      $(':submit',form).attr('disabled','disabled');
    } else {
      setConfirmUnload(true);
      $(':submit',form).removeAttr('disabled')
    }
  });
  $('input[type=submit]').click(function(){ setConfirmUnload(false); });
}
function setConfirmUnload(on) {
  window.onbeforeunload = (on) ? unloadMessage : null;
}
function unloadMessage() {
  return 'If you navigate away from this page without saving your changes, they will be lost.';
}

One of these forms needs some additional validation which I do using jQuery.validate library. 其中一种形式需要使用jQuery.validate库进行一些其他验证。 eg if i wanted to ensure the user can't double submit the form on accident by double clicking on submit or somesuch (the actual validation in question is for a credit-card form and not this simple): 例如,如果我想确保用户不能通过双击提交或类似方式两次意外提交表单(实际的验证是针对信用卡表单,而不是简单的方法):

$('form').validate({
  submitHandler: function(form) {
    $(':submit', form).attr('disabled','disabled');
    form.submit();
  }
});

Unfortunately both bits are trying to bind to submit button and they're interfering with each other such that the submit button remains disabled no matter what I do and it is impossible to submit the form at all. 不幸的是,这两个位都试图绑定到提交按钮,并且彼此干扰,因此无论我做什么,提交按钮都保持禁用状态,并且根本无法提交表单。

Is there some way to chain the validations together or something? 有什么方法可以将验证链接在一起吗? Or some other way to avoid re-writing the validation code to repeat the "did you change anything in the form" business? 还是采用其他方法来避免重新编写验证代码以重复“您是否更改了表单中的任何内容”业务?

Not sure what's wrong with your solution, but here's one that seems to work: 不知道您的解决方案出了什么问题,但是这似乎可行:

<!doctype html><!-- HTML5 ROCKS -->
<html>
<head>
<title>Usable Forms</title>
</head>
<body>

<form id="my_form" action="http://www.google.com/search">
  <input type="text" name="query">
  <input type="submit" disabled>
</form>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js">
</script>
<script>
var my_form = $('#my_form');
var original_form_values = my_form.serialize();

// Returns true if the contents of #my_form have changed since the page was
// loaded.
function MyFormChanged() {
  return my_form.serialize() != original_form_values;
}

var submitting = false;

// Warn of unsaved changes if the user tries to navigate away.
window.onbeforeunload = function() {
  // Don't stop the submission.
  if (submitting) {
    return;
  }

  if (MyFormChanged()) {
    return 'You have unsaved changes.';
  }

  // Not submitting, nor has form changed;
  // therefore, it is safe for the user to navigate away.
}

$('#my_form').on('submit', function() {
  // Just in case submit button isn't disabled for some reason.
  $('#my_form input[type=submit]').attr('disabled', '');

  if (!MyFormChanged()) {
    return false;
  }

  if (submitting) {
    return false;
  }

  submitting = true;
  return true;
});

// Toggle submit button, depending on whether there are any changes to submit.
$('#my_form input').on('change keyup', function() {
  if (!submitting && MyFormChanged()) {
    // Enable submit button.
    $('#my_form input[type=submit]').removeAttr('disabled');
  } else {
    // Disable submit button.
    $('#my_form input[type=submit]').attr('disabled', '');
  }
});
</script>

</body>
</html>

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

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