0

JqueryとAjaxを使用してZendFrameworkアプリケーションで動的フォームを構築してきましたが、問題が発生しました。奇妙な理由で、datePickerのような標準のJqueryオブジェクトの1つを追加しない限り、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>
4

1 に答える 1

2

明示的に有効にしてみてください

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

また、これを_initViewHelpers()の最後の行に設定します

Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);  
于 2012-05-23T15:46:55.513 に答える