jQuery的功能与php表单冲突 [英] Jquery function conflicts with php form

查看:144
本文介绍了jQuery的功能与php表单冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个代码:

 < script type =text / javascript> 



(函数($){

$ countForms = 1;

$ .fn.addForms = function (idform){

var myform =< table>+
< tr>+
< td>字段A(+ $ countForms + ):+
< td>< input type ='text'name ='field [+ $ countForms +] [a]'>< / td>+
< td>字段B(+ $ countForms +):< / td>+
< td>< textarea name ='field [+ $ countForms +] [ b''>< / textarea>< / td>+
< td><按钮>删除< /按钮>< / td>+
<如果(idform =='mybutton'){

< / table>;



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

};
})(jQuery);


$ b $(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 naligação。';出口;
}
mysqli_select_db($ ligax,'dados_arrays');

if(isset($ _POST ['field']))
{
echo'< table>';
foreach($ _POST ['field'] as $ diam)
{
//在这里您可以访问$ diam ['top']和$ 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 =postname =b>
< table>
< tr>
< td>字段A< / td>
< td>< input type ='text'name ='field [0] [a]'>< / td>
< td>字段B< / td>
< td>< textarea name =field [0] [b]>< / textarea>< / td>
< td><按钮>移除< /按钮>< / td>
< / tr>
< / table>
< / div>
< / div>
< button id =mybutton>新增表格< /按钮>

< div align =center>
< p>< input type =submitvalue =Registarname =registar>< / p>
< / div>

<?php

if($ ligax)mysqli_close($ ligax);

?>
< / body>

代码的第一部分是一个jquery函数,当我需要时复制表单的字段。
第二个是表单本身和接收来自表单的值的$ _POST方法。



我的问题是这部分foreach($ _POST ['field']作为$ diam)不会捕获jquery的重复字段中的值。也许是因为这是它在页面运行后出现的东西,我不知道。这就是为什么我需要帮助和建议。我怎样才能捕捉来自jquery生成输入的值?



谢谢!

解决方案

您正在寻找的词是 .noConflict API: http://api.jquery.com/jQuery.noConflict/



许多JavaScript库使用$作为函数或变量名称,就像jQuery一样。在jQuery的情况下,$是jQuery的别名,所以所有功能都可以在不使用$的情况下使用。如果我们需要在jQuery中使用另一个JavaScript库,我们可以通过调用$ .noConflict()来将控制权返回给另一个库:

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

  jQuery.noConflict(); 
(function(){
$(function(){
//更多的代码使用$作为jQuery的别名
));
})(jQuery) ;
//其他代码使用$作为别名库的别名

在no- confict模式,$快捷方式不可用,使用更长的jQuery。例如:

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

成为:

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

为了使用$的默认jQuery快捷键,您可以在代码中使用以下包装:

  jQuery(document).ready(function($){
// $()将作为别名对于这个函数里面的jQuery()
});

这个包装会导致您的代码在页面加载完成时被执行,并且$将用于调用jQuery。如果由于某种原因,您希望您的代码立即执行(而不是等待DOM就绪事件),那么您可以使用此包装方法:

 (function($){
// $()将作为jQuery()函数内部的函数
})(jQuery);

阅读良好: http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_wrappers



此外,如果您热衷于:

jQuery中的$意思是什么?



这应该有助于消除你的渴望:)可能希望这有助于!



了解$与jife in ifife而不是$



更新请注意你需要仔细检查其他地方,并做到这一点,休息冲突将得到解决。

  jQuery.noConflict(); 
(函数($){

$ countForms = 1;

$ .fn.addForms =函数(idform){

var myform =< table>+
< tr>+
< td>字段A(+ $ countForms +):< / td>+
< td>< / td>+
< td>字段B + $ countForms +):< / td>+
< td>< textarea name ='field [+ $ countForms +] [b]'>< / textarea>< / td>+
< td><按钮>删除< /按钮>< / td>+
< / tr>+
<表格>;



if(idform =='mybutton'){

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

};
})(jQuery);


first the code:

<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>

The first part of the code is a jquery function that duplicates the fields of my form when i need. The second one is the form itself and the $_POST method to receive the values coming from the form.

My problem is that this part "foreach ( $_POST['field'] as $diam )" doesn't caught the values from the duplicated fields of jquery. Maybe because it's something it appears after the page is running i don't know. That's why i need help and suggestions. How can i caught the values that come from the inputs generated by jquery?

Thank you!

解决方案

Word you are looking for is .noConflict API: http://api.jquery.com/jQuery.noConflict/

Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $. If we need to use another JavaScript library alongside jQuery, we can return control of $ back to the other library with a call to $.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

In "no-confict" mode, the $ shortcut is not available and the longer jQuery is used. For example:

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

Becomes:

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

In order to use the default jQuery shortcut of $, you can use the following wrapper around your code:

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

That wrapper will cause your code to be executed when the page finishes loading, and the $ will work for calling jQuery. If, for some reason, you want your code to execute immediately (instead of waiting for the DOM ready event), then you can use this wrapper method instead:

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

Good read: http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_wrappers

Further if you keen:

What does $ mean in jQuery?

This should help to quench your thirst :) might be hope this helps!

understanding $ vs. jQuery in iife instead of $

UPDATE please note you need to carefully check other places and do this and rest the conflict will be resolved.

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);         

这篇关于jQuery的功能与php表单冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆