簡體   English   中英

Yii2 ActiveForm Ajax POST調用了兩次

[英]Yii2 ActiveForm Ajax POST called twice

我在Yii2中是新手。 這是我的第一個認真的應用程序。

問題是我的AJAX模態表兩次檢測POST。

這就是我所說的模態形式

                Modal::begin([
                    'header' => '<h2>Hello world</h2>',
                    'toggleButton' => ['label' => 'click me'],
                    'id' => 'order_form'
                ]);


                Modal::end();
                ?>

這是我的JS代碼

$this->registerJs('
$(function() {
$( "#orderer_form" ).on( "submit", function( event ) {
  event.preventDefault();
  event.stopImmediatePropagation();
  var formData = $( this ).serialize() ;
   alert($("#orderer_form").serializeArray());
       $.ajax({
            type: "POST",
            url: "/order",
            data: formData,
            success: function(msg){
                $("#thanks").html(msg);
                $("#order_form").modal("hide"); 
            },
            error: function(){
            //alert("failure");
            }
        });
    return false;
});
});
');

這就是我渲染模態視圖的方式

<?php echo \Yii::$app->view->renderFile('@app/views/common/order_form.php', array('model'=>new salestable()));?>

這是結果

在此處輸入圖片說明

嘗試將js更改為此,對我來說,這似乎總是可以解決問題。

$('body').on('submit', '#orderer_form', function() {
    // Your code here
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM