简体   繁体   English

多个事件被触发

[英]Multiple events are being fired

i have a simple script below that creates new html forms dynamically. 我下面有一个简单的脚本,可以动态创建新的html表单。 When i attempt to submit one individually it submits the (this) clicked on form, and then submits all of the ones that came after it. 当我尝试单独提交一个时,它会提交单击的(this)表单,然后提交其后的所有表单。

Really can't see what the bug is here (code): 真的看不到这里的错误(代码):

<script type="text/javascript">
(function ($) {
$(function () {

    var addFormGroup = function (event) {
        event.preventDefault();

        var $formGroup = $(this).closest('.form-group');
        var $multipleFormGroup = $formGroup.closest('.multiple-form-group');
        var $formGroupClone = $formGroup.clone();

        $(this)
            .toggleClass('btn-success btn-add btn-danger btn-remove')
            .html('–');

        $formGroupClone.find('input').val('');
        $formGroupClone.find('.concept').text('Phone');
        $formGroupClone.insertAfter($formGroup);

        var $lastFormGroupLast = $multipleFormGroup.find('.form-group:last');
        if ($multipleFormGroup.data('max') <= countFormGroup($multipleFormGroup)) {
            $lastFormGroupLast.find('.btn-add').attr('disabled', true);
        }

       $('form').on('submit', function(event) {
        event.preventDefault();
        $(this).attr('disabled', 'disabled');
        $( this ).serialize();
        var weburl = $(this).find(".web_url").val();
        var width = $(this).find(".width").val();//$('.width').val();
        var height = $(this).find(".height").val();//$('.height').val();
        var dee = <?php echo $deet[0]->id; ?>;
        $.post( "mde", { weburl: weburl, width: width, height: height, dee: dee })
          .done(function( data ) {
             u = data;
             alert(u);
          });
        //$.get(u);  
      });

    };

    var removeFormGroup = function (event) {
        event.preventDefault();

        var $formGroup = $(this).closest('.form-group');
        var $multipleFormGroup = $formGroup.closest('.multiple-form-group');

        var $lastFormGroupLast = $multipleFormGroup.find('.form-group:last');
        if ($multipleFormGroup.data('max') >= countFormGroup($multipleFormGroup)) {
            $lastFormGroupLast.find('.btn-add').attr('disabled', false);
        }

        $formGroup.remove();
    };

    var selectFormGroup = function (event) {
        event.preventDefault();

        var $selectGroup = $(this).closest('.input-group-select');
        var param = $(this).attr("href").replace("#","");
        var concept = $(this).text();

        $selectGroup.find('.concept').text(concept);
        $selectGroup.find('.input-group-select-val').val(param);

    }

    var countFormGroup = function ($form) {
        return $form.find('.form-group').length;
    };

    $(document).on('click', '.btn-add', addFormGroup);
    $(document).on('click', '.btn-remove', removeFormGroup);

});
})(jQuery);
</script>
<script>
    $( document ).ready(function() {
      // Handler for .ready() called.
      $('form').on('submit', function(event) {
        event.preventDefault();
        $(this).attr('disabled', 'disabled');
        $( this ).serialize();
        var weburl = $(this).find(".web_url").val();
        var width = $(this).find(".width").val();//$('.width').val();
        var height = $(this).find(".height").val();//$('.height').val();
        var dee = <?php echo $deet[0]->id; ?>;
        $.post( "mde", { weburl: weburl, width: width, height: height, dee: dee })
          .done(function( data ) {
             u = data;
             alert(u);
          });
        //$.get(u);  
      });

    });
</script>

Something like this assuming the rest of the code works 假设其余代码正常工作,类似这样的事情

<script type="text/javascript">
$(function () {

    var addFormGroup = function (event) {
        event.preventDefault();

        var $formGroup = $(this).closest('.form-group');
        var $multipleFormGroup = $formGroup.closest('.multiple-form-group');
        var $formGroupClone = $formGroup.clone();

        $(this)
            .toggleClass('btn-success btn-add btn-danger btn-remove')
            .html('–');

        $formGroupClone.find('input').val('');
        $formGroupClone.find('.concept').text('Phone');
        $formGroupClone.insertAfter($formGroup);

        var $lastFormGroupLast = $multipleFormGroup.find('.form-group:last');
        if ($multipleFormGroup.data('max') <= countFormGroup($multipleFormGroup)) {
            $lastFormGroupLast.find('.btn-add').attr('disabled', true);
        }           
    };

    var removeFormGroup = function (event) {
        event.preventDefault();

        var $formGroup = $(this).closest('.form-group');
        var $multipleFormGroup = $formGroup.closest('.multiple-form-group');

        var $lastFormGroupLast = $multipleFormGroup.find('.form-group:last');
        if ($multipleFormGroup.data('max') >= countFormGroup($multipleFormGroup)) {
            $lastFormGroupLast.find('.btn-add').attr('disabled', false);
        }

        $formGroup.remove();
    };

    var selectFormGroup = function (event) {
        event.preventDefault();

        var $selectGroup = $(this).closest('.input-group-select');
        var param = $(this).attr("href").replace("#","");
        var concept = $(this).text();

        $selectGroup.find('.concept').text(concept);
        $selectGroup.find('.input-group-select-val').val(param);

    }

    var countFormGroup = function ($form) {
        return $form.find('.form-group').length;
    };

    $(document).on('click', '.btn-add', addFormGroup);
    $(document).on('click', '.btn-remove', removeFormGroup);
    $(document).on('submit','form' function(event) {
        event.preventDefault();
        $(this).attr('disabled', 'disabled');
        // $( this ).serialize(); // not used
        var weburl = $(this).find(".web_url").val();
        var width = $(this).find(".width").val();
        var height = $(this).find(".height").val();
        var dee = <?php echo $deet[0]->id; ?>;
        $.post( "mde", { weburl: weburl, width: width, height: height, dee: dee })
          .done(function( data ) {
             u = data;
             alert(u);
        });
    });
});
</script>

As they are dynamic elements use off() function along with on() function: 由于它们是动态元素,因此请使用off()函数和on()函数:

$('form').off().on('submit', function(event){
    event.preventDefault();

    ----
    ----
    ----

});

So that submit function will gets fired only on corresponding form. 因此,仅在相应的表单上会触发提交功能。

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

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