2011-03-09 317 views
0

我有一些文件:如何具有可重複使用jQuery的形式模式

* vehicule_parc.php:*

<script language=javascript src="../js/vehicule_parc.js"></script> 
<h3 class="headInfoBox" id="cch">Conso Carburant >></h3> 
<hr /> 
    <div id="cc">   
     <table cellpadding="0" cellspacing="0" border="0" class="display boxtable" id="consoTable"> 
      <thead> 
       <tr> 
        <th>Date</th> 
        <th>Heure</th> 
        <th>Quantité</th> 
        <th>Coût</th> 
        <th>Carte</th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr class="odd gradeA"> 
        <td>21/03/2011</td> 
        <td>10:00</td> 
        <td>30</td> 
        <td>40</td> 
        <td>02248</td> 
        </tr> 
      ... 
    </div><!-- cc --> 
<button id="addcc">Ajouter</button> 
<?php include 'form_conso_carb.html'; ?> 

* form_conso_carb.html:*

<div id="form_conso_carb" title="Nouvelle Consommation"> 
    <form> 
     <label for="date">Date</label>   <input type="text" name="date"  value="" /> 
     <label for="heure">Heure</label>  <input type="text" name="heure"  value="" /> 
     <label for="quantite">Heure</label>  <input type="text" name="quantite" value="" /> 
     <label for="cout">Coût</label>   <input type="text" name="cout"  value="" /> 
     <label for="carte">Carte</label>   <input type="text" name="carte"  value="" /> 
    </form> 
</div> 

* vehicule_parc.js :*

//some code before 
    J("#form_conso_carb").dialog({ 
      autoOpen : false, 
      height  : 'auto', 
      width  : 300, 
      modal  : true, 
      position : 'middle', 
      Cancel : function() { 
         J(this).dialog("close"); 
        }, 
      buttons : { 
       "Envoyer" : function() { 

          } 
      } 
     }); 

     J("#addcc") 
     .button() 
     .click(function() { 
      J("#form_conso_carb").dialog("open"); 
     }); 
     //some code after 

所以我會哈哈請將您在vehicule_parc.js中看到的代碼保存在可重複使用的文件中。但問題是代碼必須知道表的id(這裏是id="consoTable"),以表示ajax。 爲什麼不,在form_conso_carb.html也在同一個文件。

目標是簡單地將無模式表單添加到CRUD a consoTable
我希望我能理解。

回答

0

我已經從alsacreations (FR)該溶液中,我必須封裝代碼,如這個例子,並把它放在一個文件:

var bibliJsActif = (function() { 
    // Membres privés 
    function init() { 
    bibliJsActif.ajouterClasse(document.body, bibliJsActif.nouvelleClasse); 
    } 

    if (document.getElementById && document.createTextNode) { 
    window.onload = init; 
    } 

    // Membres publics 
    return { 
    "ajouterClasse": function(element, classe) { 
     if (element.className) { 
     element.className += " "; 
     } 

     element.className += classe; 
    }, 

    "nouvelleClasse": "jsActif" 
    }; 
})(); 
0

使它成爲一個函數,並使用this在點擊事件來引用當前對象,並通過其作爲一個參數:

function showDialog(element) { 
     $(element).dialog({ 
      autoOpen : false, 
      height  : 'auto', 
      width  : 300, 
      modal  : true, 
      position : 'middle', 
      Cancel : function() { 
         J(this).dialog("close"); 
        }, 
      buttons : { 
       "Envoyer" : function() { 

          } 
      } 
     }); 
     } 

     J("#addcc") 
     .button() 
     .click(function() { 
      showDialog(this); 
     }); 
    } 
+0

不工作,J(「#布拉布拉」)對話()必須在頁面開始加載,否則'form_conso_carb.html'中所示'vehicule_parc.php' – canardman 2011-03-09 14:14:51

+0

對不起,我很困惑。我想我誤解了你需要做的事情。我仍然不確定。 – 2011-03-09 14:19:44

+0

在我想要的地方添加'J(ID).dialog()',並在當前頁面上使用與表ID相對應的預定義ID – canardman 2011-03-09 14:59:21