簡體   English   中英

如何在jQuery ajax中添加新的ZF2'Zend \\ Form \\ Element

[英]How to add a new ZF2' Zend\Form\Element within jQuery ajax

我想在jQuery ajax中添加一個新的ZF2'Zend \\ Form \\ Element。 但是當我使用Zend Form時,我不知道如何制作它。 這是add.phtml文件。

<script type="text/javascript">
 $(document).ready(function(){
    $(".pid").change(function(){
    var id = $('.pid').val();
    var $_this = $(this);
     $.ajax({
            type:"POST",
            url:"<?php echo $this->url('Element/default',array('action'=>'change'));?>",
            data:"pid="+id,
            dataType:'json',
            async:false,
            success:function(data){
                if(data.response){
                   //here I want to add a new select  component after select conponent "pid" using like "$this->formRow($form->get('mid'))" or else .   
                }
            }
    });
  });
});
</script>

以下是html的其余部分。

<?php
$title = 'add';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>

<?php
$form = $this->form;

$form->setAttribute('action', $this->url(
'Element/default',
array(
    'action'     => 'add'
 )
));
$form->prepare();

echo $this->form()->openTag($form); 
echo $this->formRow($form->get('pid'));
echo $this->formRow($form->get('name'));
echo $this->formRow($form->get('desc'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();

如何在jquery ajax中添加新的zend表單元素? 謝謝。

您具有使用ajax接收數據的腳本,並且此腳本應接收view / html。 您需要控制器/操作來呈現數據並作為響應返回。

use Zend\View\Model\JsonModel;    
//some controller
public function changeAction(){ // your requested action
    //1. get partial helper to rendering html;
    $partial = $this->getServiceLocator()->get('ViewHelperManager')->get('partial');
    $form = new Form();// your form
    //2. render html
    $html = $partial('path/to/your/file/phtml',['form'=>$form]);
    //3. return data as JSON because in your ajax configuration dataType: 'json'
    return new JsonModel([
        'html' => $html,
    ]);
}

您的js成功函數中應為:

success:function(data){
    if(data.html){
         $_this.find('blockToAppend').append(data.html);
    }
}

暫無
暫無

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

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