2012-05-23 57 views
0

我一直在使用Jquery和Ajax在我的Zend Framework应用程序中构建一个动态表单,但是我遇到了一个问题。由于一些奇怪的原因,Jquery不能在我的页面上工作,除非我添加一个像datePicker这样的标准Jquery对象。Zend Framework Jquery加载有一些奇怪的行为

每当我添加日期选择器时,我的代码工作得很好,当我删除日期选择器时,它给出的错误是$(document).ready(function()变量未定义。这本身是正确的考虑到jquery没有被加载,因此变量对浏览器没有意义。

我有库,引导和echo $this->Jquery();都设立了适当否则日期选取器将不起作用,当日期选择器出现时,它与其他代码的工作完全表明它是所有正确设置。

是否有其他人遇到过Jquery不能正确加载的问题?

我的引导代码:

protected function _initViewHelpers() 
{ 
    $view = new Zend_View(); 
    $view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper'); 
    $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer'); 
    $viewRenderer->setView($view); 

和我的布局代码:

<?php 
echo $this->jQuery(); 
echo $this->layout()->content; 
?> 

我的application.ini也有autoloaderNamespaces[] = "ZendX"线

这里是jQuery代码在我看来:

<p>All fields are required.</p> 

<?= $this->form; ?> 

<script type="text/javascript"> 

$(document).ready(function() { 

    $("#addElement").click( 
     function() { 
      ajaxAddField(); 
     } 
    ); 

    $("#removeElement").click(
     function() { 
      removeField(); 
     } 
    ); 
    } 
); 

// Get value of id - integer appended to dynamic form field names and ids 
var id = $("#id").val(); 

// Retrieve new element's html from controller 
function ajaxAddField() { 
    $.ajax(
    { 
     type: "POST", 
     url: "<?=$this->url(array('action' => 'newfield', 'format' => 'html'));?>", 
     data: "id=" + id, 
     success: function(newElement) { 

     // Insert new element before the Add button 
     $("#addElement-label").before(newElement); 

     // Increment and store id 
     $("#id").val(++id); 
     } 
    } 
); 
} 

function removeField() { 

    // Get the last used id 
    var lastId = $("#id").val() - 1; 

    // Build the attribute search string. This will match the last added dt and dd elements. 
    // Specifically, it matches any element where the id begins with 'newName<int>-'. 
    searchString = '*[id^=newName' + lastId + '-]'; 

    // Remove the elements that match the search string. 
    $(searchString).remove() 

    // Decrement and store id 
    $("#id").val(--id); 
} 

</script> 

回答

2

尝试启用它明确

$this->jQuery()->enable(); 

而且还_initViewHelpers()

Zend_Controller_Action_HelperBroker::addHelper($viewRenderer); 
+0

简单而有效的设置这个在最后一行!非常感谢 :) –