2012-07-11 53 views
0

第一代碼Jquery的函數的衝突:用PHP形式

<script type="text/javascript"> 



(function($){ 

$countForms = 1; 

     $.fn.addForms = function(idform){ 

        var myform = "<table>"+ 
        " <tr>"+ 
        "  <td>Field A ("+$countForms+"):</td>"+ 
        "  <td><input type='text' name='field["+$countForms+"][a]'></td>"+ 
        "  <td>Field B ("+$countForms+"):</td>"+ 
        "  <td><textarea name='field["+$countForms+"][b]'></textarea></td>"+ 
        "  <td><button>remove</button></td>"+ 
        " </tr>"+ 
        "</table>"; 



        if(idform=='mybutton'){ 

         myform = $("<div>"+myform+"</div>"); 
         $("button", $(myform)).click(function(){ $(this).parent().parent().remove(); }); 
         $(this).append(myform); 
         $countForms++; 
        } 

     }; 
})(jQuery);   



$(function(){ 
    $("#mybutton").bind("click", function(e){ 
    e.preventDefault(); 
    var idform=this.id; 

     if($countForms<3){ 
      $("#container").addForms(idform); 
     }  
    }); 
}); 

<?php 
$ligax=mysqli_connect('localhost','root'); 
if(!$ligax){ 
    echo '<p> Falha na ligação.'; exit; 
} 
mysqli_select_db($ligax,'dados_arrays'); 

    if (isset($_POST['field'])) 
{ 
    echo '<table>'; 
    foreach ($_POST['field'] as $diam) 
    { 
     // here you have access to $diam['top'] and $diam['bottom'] 
     echo '<tr>'; 
     echo ' <td>', $diam['a'], '</td>'; 
     echo ' <td>', $diam['b'], '</td>'; 
     echo '</tr>'; 
    } 
    echo '</table>'; 
}  
?> 

<body> 

<div id="container"><div> 
<form method="post" name="b" > 
<table> 
    <tr> 
     <td>Field A</td> 
     <td><input type='text' name='field[0][a]'></td> 
     <td>Field B</td> 
     <td><textarea name="field[0][b]"></textarea></td> 
     <td><button>remove</button></td> 
    </tr> 
</table> 
</div> 
</div> 
<button id="mybutton">add form</button> 

<div align="center"> 
<p><input type="submit" value="Registar" name="registar"></p> 
</div> 

<?php 

    if($ligax) mysqli_close($ligax); 

?> 
</body> 

代碼的第一部分是複製我的表單的字段時,我需要一個jquery功能。 第二個是窗體本身和$ _POST方法來接收來自窗體的值。

我的問題是,這部分「foreach($ _POST ['field'] as $ diam)」沒有捕獲到來自jquery重複字段的值。也許是因爲這是它在頁面運行後出現的東西,我不知道。這就是爲什麼我需要幫助和建議。我怎樣才能捕捉來自jQuery產生的輸入值?

謝謝!

您正在尋找
+0

你可以轉儲後變量,看看你提交表單後它拿着什麼? – 2012-07-12 13:27:35

+0

我在html的輸入上插入「aaa」和「bbb」,在jquery生成的輸入上插入「ccc」,「ddd」。 dump變量的結果:array(2){[「field」] => array(1){[0] => array(2){[「a」] => string(3)「aaa」[「b 「] => string(3)」bbb「}} [」registar「] => string(8)」Registar「}沒有跡象從jquery的輸入:/ – user1511579 2012-07-12 16:06:40

回答

1

字是.noConflict API:http://api.jquery.com/jQuery.noConflict/

許多JavaScript庫使用$作爲函數或變量名,就像jQuery不會。在jQuery的情況下,$是jQuery的別名,所以所有功能都可以在不使用$的情況下使用。如果我們需要使用另一個JavaScript庫jQuery的旁邊,我們可以返回$控制權交還給其他庫到$ .noConflict()的調用:

http://api.jquery.com/jQuery.noConflict/

jQuery.noConflict(); 
(function($) { 
    $(function() { 
    // more code using $ as alias to jQuery 
    }); 
})(jQuery); 
// other code using $ as an alias to the other library 

在「無confict」模式下,$快捷方式不可用,並使用更長的jQuery。例如:

$(document).ready(function(){ 
    $(#somefunction) ... 
}); 

變爲:

jQuery(document).ready(function(){ 
    jQuery(#somefunction) ... 
}); 

爲了使用$默認jQuery的快捷方式,你可以用在你的代碼如下包裝:

jQuery(document).ready(function($) { 
    // $() will work as an alias for jQuery() inside of this function 
}); 

那包裝將導致您的代碼在頁面加載完成時執行,並且$將用於調用jQuery。如果由於某種原因,你想你的代碼,立即執行(而不是等待DOM ready事件),那麼你可以使用此包裝方法代替:

(function($) { 
    // $() will work as an alias for jQuery() inside of this function 
})(jQuery); 

良好讀取:http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_wrappers

另外,如果你渴望:

What does $ mean in jQuery?

這將有助於解渴:)可能是希望這有助於!

understanding $ vs. jQuery in iife instead of $

UPDATE請注意,你需要仔細檢查一下其他地方,做到這一點,休息的矛盾將得到解決。

jQuery.noConflict(); 
(function($){ 

$countForms = 1; 

     $.fn.addForms = function(idform){ 

        var myform = "<table>"+ 
        " <tr>"+ 
        "  <td>Field A ("+$countForms+"):</td>"+ 
        "  <td><input type='text' name='field["+$countForms+"][a]'></td>"+ 
        "  <td>Field B ("+$countForms+"):</td>"+ 
        "  <td><textarea name='field["+$countForms+"][b]'></textarea></td>"+ 
        "  <td><button>remove</button></td>"+ 
        " </tr>"+ 
        "</table>"; 



        if(idform=='mybutton'){ 

         myform = $("<div>"+myform+"</div>"); 
         $("button", $(myform)).click(function(){ $(this).parent().parent().remove(); }); 
         $(this).append(myform); 
         $countForms++; 
        } 

     }; 
})(jQuery);   
+0

該死的,jquery問題又來了?我擔心的是,任何更改都會與我正在使用的其他jquery插件發生衝突。無論如何,關於你的提示,你給的最後一個是我正在使用的那個,就像你在上面的代碼中看到的那樣。她不應該工作正常嗎? – user1511579 2012-07-11 21:57:56

+0

@ user1511579大聲笑,Neah不是一個Jquery概率,它的'精彩'和思想滿滿,我們用'noConflict' API來處理任何衝突':'它只有幾行改變,你將會搖擺!在腳本的開始處使用'Jquery.noConflict',希望這有助於。 – 2012-07-11 22:03:05

+0

只是這個嗎? jQuery.noConflict(); 剩下的代碼呢?沒有變化? – user1511579 2012-07-11 22:09:51

0

你的問題在於你在附加字段的地方。您正在窗體外添加表單字段。

你的邏輯,以追加形式:

if(idform=='mybutton'){ 
    myform = $("<div>"+myform+"</div>"); 
    $("button", $(myform)).click(function(){ 
     $(this).parent().parent().remove(); 
    }); 
    $(this).append(myform); 
    $countForms++; 
} 

應該是:

if(idform=='mybutton'){ 
    myform = $("<div>"+myform+"</div>"); 
    $("button", $(myform)).click(function(){ 
     $(this).parent().parent().remove(); 
    }); 
    $("formelement").append(myform); 
    $countForms++; 
} 

假設你修改的形式標記分配的formelement的ID。根據需要進行調整。