的形式使用jQuery的序列化()和AJAX发送部分 [英] Sending parts of form using jQuery serialize() and AJAX

查看:125
本文介绍了的形式使用jQuery的序列化()和AJAX发送部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过AJAX使用jQuery的序列化发送的一种形式的部分。该表格​​有16个文本框。我有4个按钮。该 BUTTON0 发送文本框0,1,2,3,和按钮1 发送文本框4,5,6, 7,等等等等。我该怎么办呢?

HTML

 < HTML>
 < HEAD>
    < META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8>
    <冠军>序列化< /标题>
    <脚本类型=文/ JavaScript的SRC =jQuery的-1.9.1.min.js>< / SCRIPT>
 < /头>
 <身体GT;
    <形式ID ='miForm级='miForm的名字='miForm'行动='env.php'方法='POST'>

    < /形式GT;
 < /身体GT;
< / HTML>
 

jQuery的:

  $(文件)。就绪(函数(){
        对于(i = 0; I< 16;我++){
            $('表')追加('坎波texto+ I +'<输入类型=文本ID =TXT。+ I +'值=坎普'+ I +'只读=YES/&GT ;< BR>');
        }
        对于(I = 0; I&4;;我++){
            $('表')追加('<输入类型=按钮ID =butEnv'+ I +'值=投递+ I +/>< BR>');
        }
        $('表')追加('<输入类型=按钮ID =butGen值=投递全球/>< BR>');

    });
 

解决方案

如果你真的想留下来,只有一种形式尝试像我做的这个小提琴

创建子部件的形式。

 <形式GT;
    < D​​IV ID =第一>
        <输入名称=tbox1类型=文本>
        <输入名称=tbox2类型=文本>
        <输入名称=tbox3类型=文本>
        <输入ID =Button1的类型=按钮值=按钮1>
    < / DIV>
    < D​​IV ID =第二>
        <输入名称=tbox4类型=文本>
        <输入名称=tbox5类型=文本>
        <输入名称=tbox6类型=文本>
        <输入ID =按钮2型=按钮值=按钮2>
    < / DIV>
< /形式GT;
 

然后把刚才选中的部分中的所有元素:

  $(文件)。就绪(函数(){
    $('#按钮1')。在('点击',函数(){
        警报($('#第一个输入')序列化());
    });

      $('#按钮2)。在('点击',函数(){
        警报($('#第二个输入')序列化());
    });
});
 

当然,如果你也有,你必须将它们添加到选择器选择框。例如:

  $('#第二个输入,#second选择)。序列化()
 

I'm trying to send parts of one form by AJAX using jQuery's serialize. The form has 16 textfields. I have 4 buttons. The button0 sends the textfields 0,1,2,3, and button1 sends the textfields 4,5,6,7, etc etc. How can I do it?

HTML

<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Serialize</title>  
    <script type="text/javascript" src="jquery-1.9.1.min.js"></script>   
 </head>
 <body>
    <form id='miForm' class='miForm' name='miForm' action='env.php' method='POST'>

    </form>
 </body>
</html>

jQuery:

     $(document).ready(function(){
        for(i=0;i<16;i++){
            $('form').append('Campo de texto '+i+'<input type="text" id="txt'+i+'" value="Campo '+i+'" readonly="yes"/><br>');
        }
        for(i=0;i<4;i++){
            $('form').append('<input type="button" id="butEnv'+i+'" value="Enviar'+i+'"/><br>');
        }
        $('form').append('<input type="button" id="butGen" value="Enviar Global"/><br>');

    });

解决方案

If you really want to stay with only one form try something like I did in this fiddle.

Create sub parts for your form.

<form>
    <div id="first">
        <input name="tbox1" type="text">
        <input name="tbox2" type="text">
        <input name="tbox3" type="text">    
        <input id="button1" type="button" value="button1">  
    </div>
    <div id="second">
        <input name="tbox4" type="text">
        <input name="tbox5" type="text">
        <input name="tbox6" type="text">    
        <input id="button2" type="button" value="button2">  
    </div>
</form>

And then just select all the elements of the parts:

$(document).ready(function() {
    $('#button1').on('click', function() {
        alert($('#first input').serialize());
    });

      $('#button2').on('click', function() {
        alert($('#second input').serialize());
    });
});

Of course if you also have select boxes you have to add them to the selectors. For example:

$('#second input, #second select').serialize()

这篇关于的形式使用jQuery的序列化()和AJAX发送部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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