如何“提交”使用jQuery的表单的一部分 [英] howto "submit" a part of a form using jQuery

查看:72
本文介绍了如何“提交”使用jQuery的表单的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张像电子表格一样的表格。当用户离开一行时,我想使用jQuery Ajax将该行的字段提交给服务器。页面是一个大的形式,所以这不是一个javascript点击提交按钮场景 - 表格很大,我只想发送一小部分内容,原因是速度。

I'm got a form laid out like a spreadsheet. When the user leaves a row, I want to submit fields from that row to the server using jQuery Ajax. The page is one large form, so this isn't really a javascript clicking the submit button scenario - the form is huge and I only want to send a small portion of the content for reasons of speed.

我已经编写了代码来识别行并迭代行中的字段。我的问题是如何构建dat对象以便提交一些可以理解的东西我可以在服务器端进行反汇编和存储。

I've got the code written that identifies the row and iterates through the fields in the row. My issue is how to build the dat object in order to submit something comprehensible I can disassemble and store at the server end.

目前我的代码看起来像这样

At the moment my code looks like this

var dat=[];
$("#" + finalrow).find("input").each(function () {
    var o = $(this).attr("name");
    var v = $(this).val();
    dat.push({ o: v });
});
$.ajax({
    url: 'UpdateRowAjax',
    dataType: 'json',
    type: 'POST',
    data: dat ,
    success: function (data) {
        renderAjaxResponse(data);
    }
});

汇编数据根本不起作用。那么我应该如何构建那个dat对象,以使其看起来尽可能像表单提交一样。

The assembling of dat doesn't work at all. So how should I build that dat object in order for it to "look" as much like a form submission as possible.

推荐答案

您可以将包含要发送的数据的元素添加到jQuery集合,然后在该对象上调用 serialize 方法。它将返回一个可以发送到服务器的参数字符串。

You can add the elements that contain the data you want to send to a jQuery collection, and then call the serialize method on that object. It will return a parameter string that you can send off to the server.

var params = $("#" + finalrow).find("input").serialize();

$.ajax({
    url: 'UpdateRowAjax',
    type: 'POST',
    data: params ,
    success: function (data) {
        renderAjaxResponse(data);
    }
});

这篇关于如何“提交”使用jQuery的表单的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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