【问题标题】:yii ajax submit formyii ajax 提交表单
【发布时间】:2012-12-22 10:27:43
【问题描述】:

控制器:

public function actionCreate() {
    $model = new SiteWidget;
    if (isset($_POST['SiteWidget'])) {
        $model -> attributes = $_POST['SiteWidget'];
        if ($model -> validate()) {
            if ($model -> save())
            {
                return 'OK';
            }       
        }
    }
    $this -> render('create', array('model' => $model, ));
}

查看:

<div class="form">  
    <?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'site-widget-form',
        'enableAjaxValidation'=>false,
        'htmlOptions'=>array(
                         'onsubmit'=>"return false;",
                         'onkeypress'=>" if(event.keyCode == 13){ send(); } "
                                 ),
    )); ?>

    <p class="note">
        Fields with <span class="required">*</span> are required.
    </p>

    <?php echo $form -> errorSummary($model); ?>

    <div class="row">
        <?php echo $form -> labelEx($model, 'title'); ?>
        <?php echo $form -> textField($model, 'title', array('size' => 60, 'maxlength' => 256)); ?>
        <?php echo $form -> error($model, 'title'); ?>
    </div>

    <div class="row">
        <?php echo $form -> labelEx($model, 'type'); 
            echo $form -> dropDownList($model, 'type',  SiteWidget::getConstants('W_', 'SiteWidget'));
            echo $form -> error($model, 'type'); ?>
    </div>
    <div class="row">
        <?php echo $form -> labelEx($model, 'status'); ?>
        <?php echo $form -> checkBox($model, 'status'); ?>
        <?php echo $form -> error($model, 'status'); ?>
    </div>
    <div class="row buttons">
        <?php echo CHtml::submitButton($model -> isNewRecord ? 'Create' : 'Save',array('onclick'=>'send();')); ?>
    </div>
    <?php $this -> endWidget(); ?>
</div>

<script language="JavaScript">
function send(){
    var data=$("#site-widget-form").serialize();

      $.ajax({
           type: 'POST',
            url: '<?php echo Yii::app()->createAbsoluteUrl("siteWidget/create"); ?>',
            data:data,
            success:function(data){
                        alert(data);
                      },
           error: function(data) {
                 alert("Error occured.please try again");
            },
          dataType:'html'
      });
}
</script>

为什么 ajax 不起作用? 我哪里做错了?

【问题讨论】:

    标签: ajax yii


    【解决方案1】:

    解决方案:

    <?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'site-widget-form',
        'enableAjaxValidation'=>true,
        'htmlOptions'=>array(
                         'onsubmit'=>"return false;",
                         'onkeypress'=>" if(event.keyCode == 13){ send(); } "
                                 ),
    )); ?>
    

    查看更多ajax submition button with valiation here

    【讨论】:

      【解决方案2】:

      Data 应该是一个 JavaScript 对象,也许 serialize() 会产生问题? 这里是jQuery - AJAX article with working example :)

      【讨论】:

      • 请在此处添加来自网站的相关信息,并提供链接仅供参考。
      • 不,序列化是正确的,但是表单还没有提交到写入的地址。不知道为什么?!
      【解决方案3】:

      尝试使用 Yii ajaxSubmitButton

      【讨论】:

        猜你喜欢
        • 2016-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-17
        相关资源
        最近更新 更多