代码引用其他表单时的Javascript或表单验证

代码引用其他表单时的Javascript或表单验证,javascript,forms,validation,Javascript,Forms,Validation,下面使用表单按钮将照片标记为不同的质量。如果选择了Quality1或Quality2,则php代码应该触发。但是,如果选择删除而不是指定质量,我希望在执行脚本之前进行确认。我不希望验证,除非按下删除按钮。由于表单代码甚至会更改css,如果按delete键,我根本不希望代码运行,直到确定是否已确认。 我尝试了一些方法,但表单是即时运行的,可能是因为$'form'。在'click','input[type=submit]'上,函数E{line.我从中剥离了我的尝试,因为它只是混淆了问题。我想了解验证

下面使用表单按钮将照片标记为不同的质量。如果选择了Quality1或Quality2,则php代码应该触发。但是,如果选择删除而不是指定质量,我希望在执行脚本之前进行确认。我不希望验证,除非按下删除按钮。由于表单代码甚至会更改css,如果按delete键,我根本不希望代码运行,直到确定是否已确认。 我尝试了一些方法,但表单是即时运行的,可能是因为$'form'。在'click','input[type=submit]'上,函数E{line.我从中剥离了我的尝试,因为它只是混淆了问题。我想了解验证的语法。非常感谢任何帮助。我剥离了图像的代码,因为这在示例中不需要

Javascript

HTML

这是小提琴:

您可以改为在表单上使用submit处理程序,这样可以防止发送表单,在实际提交表单之前执行所需的所有验证:

$('form').submit( function(e) {
  // Prior to the submission, check if we are in
  // the delete button and raise the "Are you sure" button
  if (validations)
    return false; // This will prevent the submission
  else {
    // Run your PHP code
    $.ajax({
      type: 'post',
      url: "mycode.php", 
      data: $(this).parent().serialize(),
    });
  }
});

更多信息

只需要一点技巧。数据:$this.parent.serialize,需要是数据:$That.parent.serialize,-现在可以工作了。干杯!哦,对不起,我错过了。很高兴帮助你!如果有帮助,请接受答案。
<form>
    <input name="num" type="hidden" value="101">
    <input name="shw" type="hidden" value="1">
    <input type="submit" value="Delete" class="c_off" data-image="67">
</form>

<form>
    <input name="num" type="hidden" value="101">
    <input name="shw" type="hidden" value="2">
    <input type="submit" value="Quality A" class="c_on" data-image="67">
</form>

<form>
    <input name="num" type="hidden" value="101">
    <input name="shw" type="hidden" value="3">
    <input type="submit" value="Quality B" class="c_off" data-image="67">
</form>
$(function() {
    $('form').on('click', 'input[type="submit"]', function(e) {
        if($(this).val() == 'Delete') {
            if (confirm("Delete it?")) {
              doAjax($(this));
            } 
        } else {
            doAjax($(this));
        }

        e.preventDefault();
    });
    function doAjax(that){
        $.ajax({
            type: 'post',
            url: "mycode.php", 
            data: $(this).parent().serialize(),
        });
        var clicked = that,
            imageName = clicked.data("image");
        clicked.removeClass("c_off").addClass("c_on");
        $('input[type="submit"]').each(function() {
            var self = $(this);
            if (!clicked.is(self)) {
                if (self.hasClass("c_on") && imageName == self.data("image"))
                    self.removeClass("c_on").addClass("c_off");
                }
        });
    } 
});
$('form').submit( function(e) {
  // Prior to the submission, check if we are in
  // the delete button and raise the "Are you sure" button
  if (validations)
    return false; // This will prevent the submission
  else {
    // Run your PHP code
    $.ajax({
      type: 'post',
      url: "mycode.php", 
      data: $(this).parent().serialize(),
    });
  }
});