jQuery序列化div内容以发布输入数据 [英] jquery serialize a div content to post input data

查看:44
本文介绍了jQuery序列化div内容以发布输入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用通常在窗体上使用的jQuery.serialize()函数来对div中的数据进行序列化.

I'm trying to use the jQuery.serialize() function, generally used on form, to serialize data inside a div.

在asp.net Webforms中工作时,我不能使用form标签,因为它会将所有内容包装在表单中,因此嵌套表单不起作用.

I can't use the form tag as I'm working in asp.net webforms that wraps everything inside a form, so nested form doesn't work.

这是我的js代码:

$('div.request input.submit-button').on('click', function () {
var form = $(this).parent();

            $.ajax({
            type: "POST",
            url: '/API/InfoRequest.ashx',
            data: $(form).serialize(),
            dataType: "text",
            success: function (response) {           

            }
        })
});

但是$(form).serialize()返回一个空字符串.

but $(form).serialize() return an empty string.

这是HTML:

<div role="form" id="sidebar-form"  class="request">
<div class="form-group has-feedback">
    <label for="name3">Nome</label>
    <input type="text" class="form-control" id="name3" placeholder="Nome e Cognome" name="name3">
    <i class="fa fa-user form-control-feedback"></i>
</div>
<div class="form-group has-feedback">
    <label for="email3">Email</label>
    <input type="email" class="form-control" id="email3" placeholder="Indirizzo Email" name="email3">
    <i class="fa fa-envelope form-control-feedback"></i>
</div>
<div class="form-group">
    <label>Categoria</label>
    <select class="form-control" id="category">
        <option value="Sales">Vendita</option>
        <option value="Support">Tecnica</option>
        <option value="Lorem">Logistica</option>
        <option value="Ipsum sit">Ipsum sit</option>
        <option value="Dolor amet">Dolor amet</option>
    </select>
</div>
<div class="form-group has-feedback">
    <label for="message3">Messaggio</label>
    <textarea class="form-control" rows="4" id="message3" placeholder="" name="message3"></textarea>
    <i class="fa fa-pencil form-control-feedback"></i>
</div>
<input type="button" value="INVIA" class="submit-button btn btn-primary">
</div>

我只想检索表单字段,例如输入,选择,文本区域,将它们以urlencoded形式发布,然后在调用的ASHX处理程序中读取它们.

I want to retrieve only the form fields, like input, select, textarea, post them as form urlencoded and then read them in the called ASHX handler.

是否可以使用serialize()?

Is it possible using serialize()?

推荐答案

尝试

$('#sidebar-form :input, #sidebar-form textarea,  #sidebar-form select').serialize();

$("#sidebar-form").find("select, textarea, input").serialize()

这篇关于jQuery序列化div内容以发布输入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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