2012-09-07 16 views
0

我有兩個代碼位,我想一起工作,但我不管理,整合這個jQuery代碼插入JavaScript的正確...包含的jQuery JavaScript中

的JavaScript:

(function(){ 

// Creates the plugin 
tinymce.create('tinymce.plugins.mygallery', { 

    // Creates control instances based on the control's ID. 
    createControl : function(id, controlManager) { 
     if (id == 'mygallery_button') { 

      // Creates the button 
      var button = controlManager.createButton('mygallery_button', { 
       title : 'MyGallery Shortcode', // Title of the button 
       image : '../wp-includes/images/smilies/icon_mrgreen.gif', // Path to the button's image 
       onclick : function() { 

        // Triggers the thickbox 
        var width = jQuery(window).width(), H = jQuery(window).height(), W = (720 < width) ? 720 : width; 
        W = W - 80; 
        H = H - 184; 
        tb_show('My Gallery Shortcode', '#TB_inline?width=' + W + '&height=' + H + '&inlineId=mygallery-form'); 
       } 
      }); 
      return button; 
     } 
     return null; 
    } 
}); 

// Registers the plugin 
tinymce.PluginManager.add('mygallery', tinymce.plugins.mygallery); 


// Executes this when the DOM is ready 
jQuery(function(){ 


// Creates a form to be displayed everytime the button is clicked 
    var form = jQuery('<div id="mygallery-form"><table id="mygallery-table" class="form-table">\ 
     <form id="myForm">\ 
      <div id="input1" style="margin-bottom:4px;" class="clonedInput">\ 
       Name: <input type="text" name="name1" id="name1" />\ 
      </div>\ 
      <div>\ 
       <input type="button" id="btnAdd" value="add another name" />\ 
       <input type="button" id="btnDel" value="remove name" />\ 
      </div>\ 
     </form>\ 

     </div>'); 

    […] 
}); 
})() 

jQuery的:

$(document).ready(function() { 
     $('#btnAdd').click(function() { 
      var num  = $('.clonedInput').length; // how many "duplicatable" input fields we currently have 
      var newNum = new Number(num + 1);  // the numeric ID of the new input field being added 

      // create the new element via clone(), and manipulate it's ID using newNum value 
      var newElem = $('#input' + num).clone().attr('id', 'input' + newNum); 

      // manipulate the name/id values of the input inside the new element 
      newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum); 

      // insert the new element after the last "duplicatable" input field 
      $('#input' + num).after(newElem); 

      // enable the "remove" button 
      $('#btnDel').attr('disabled',''); 

      // business rule: you can only add 5 names 
      if (newNum == 5) 
       $('#btnAdd').attr('disabled','disabled'); 
     }); 

     $('#btnDel').click(function() { 
      var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have 
      $('#input' + num).remove();  // remove the last element 

      // enable the "add" button 
      $('#btnAdd').attr('disabled',''); 

      // if only one element remains, disable the "remove" button 
      if (num-1 == 1) 
       $('#btnDel').attr('disabled','disabled'); 
     }); 

     $('#btnDel').attr('disabled','disabled'); 
}); 

FYI:該JS創建了一個表格(WordPress的),一個彈出式,並與jQuery的片段,我想實現,用戶可以動態地添加多個輸入字段的功能。

感謝您的幫助!

+0

什麼是你的問題?考慮使用[jsfiddle.net](http://www.jsfiddle.net)來說明您可能遇到的任何錯誤。 – adamdport

+0

僅供參考.. jQuery和Javascript沒有區別。他們實際上是相同的確切語言。但是jQuery的是建立關閉的JavaScript庫,這樣你就可以很容易地開發你的了兼容性問題的應用程序與喜中有憂的前端,也不必花費大量的時間編寫大量代碼,什麼內jQuery的每一個功能呢用一個簡單的'.functionName()'因此把jQuery放在jQuery中的JavaScript或Javascript是一個多餘的問題。 – chris

回答

1

您可以將JavaScript代碼後,把jQuery的片段。這應該不是問題,因爲你的jQuery代碼只是綁定事件。

不過,我說不出哪裏#btnAdd和#btnDel是。該問題可能是運行jQuery代碼後創建的元素,因此,當你叫這樣的: $('#btnAdd').click(function(){...}); 元素#btnAdd是不存在。

試試這個: $(document).on('click', '#btnAdd', function(){...});

這將綁定click事件記錄(這是始終存在的),而不是#btnAdd。 爲了雖然提高性能,你應該把它綁定到#btnAdd的父母,你肯定知道是存在當文檔就緒。