有没有办法使用 jQuery 的序列化表单字段并修剪字段中的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/218794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:48:06  来源:igfitidea点击:

Is there a way to use jQuery's serialize form fields and trim the value in the fields?

jqueryformsserializationtrim

提问by Jared

I have a form that uses jQuery to submit an ajax post and it serializes the form that is sent up. The code looks like this:

我有一个使用 jQuery 提交 ajax 帖子的表单,它会序列化发送的表单。代码如下所示:

var form = $("form");
var action = form.attr("action");
var serializedForm = form.serialize();
$.post(action, serializedForm, function(data)
{
  ...
});

The problem here is that if a field has trailing white space, the serialize function will turn those spaces to plus (+) signs, when they should be stripped.

这里的问题是,如果一个字段有尾随空格,serialize 函数会将这些空格转换为加号 (+),当它们应该被删除时。

Is there a way to get the fields trimmed withoutdoing the following:

有没有办法在执行以下操作的情况下修剪字段:

$("#name").val( jQuery.trim( $("#name") ) );

回答by Jethro Larson

You could try looping through the object and triming everything.

您可以尝试遍历对象并修剪所有内容。

//Serialize form as array
var serializedForm = form.serializeArray();
//trim values
for(var i =0, len = serializedForm.length;i<len;i++){
  serializedForm[i] = $.trim(serializedForm[i]);
}
//turn it into a string if you wish
serializedForm = $.param(serializedForm);

回答by micahwittman

Trim all <input>and <textarea></textarea>element values in the DOM:

修剪DOM 中的所有<input><textarea></textarea>元素值:

$('input, textarea').each(function(){
    $(this).val(jQuery.trim($(this).val()));
});

回答by Ulf Lindback

A little late, but this was probably what you wanted:

有点晚了,但这可能是您想要的:

var form = $("form");
var action = form.attr("action");
var formArr = form. serializeArray();
jQuery.each(formArr , function(i, field) {
  formArr[i].value = $.trim(field.value);
});
var serializedForm = $.param(formArr);
$.post(action, serializedForm, function(data)
{
  ...
});

回答by Josh Bush

You could loop over all of the inputs and trim before submitting.

您可以在提交之前遍历所有输入并进行修剪。

$("input, textarea").each(function(){
   $(this).val(jQuery.trim($(this).val()));
});

回答by Jared

Neither of those solutions work, since they actually change the form fields on the page. I just want to modify the value of the field that without changing what the user typed in.

这些解决方案都不起作用,因为它们实际上更改了页面上的表单字段。我只想修改字段的值,而不更改用户输入的内容。

回答by Bryan A

One thing you could do is have a separate form with hidden values, and store the actual, trimmed, form values in the hidden values when the user submits, then you can serialize the "hidden" form and post that. Just an idea.

您可以做的一件事是拥有一个带有隐藏值的单独表单,并在用户提交时将实际的、修剪过的表单值存储在隐藏值中,然后您可以序列化“隐藏”表单并发布它。只是一个想法。

回答by Bryan A

If you are using ASP.NET where you can have only one form per page, you can submit only the values of a given DIV as follows:

如果您正在使用 ASP.NET,其中每页只能有一个表单,则您只能提交给定 DIV 的值,如下所示:

var dataString = "source=contactDiv";
dataString += getDataString(divId, "input"); // add inputs
dataString += getDataString(divId, "select"); //add select elements

then post the update as follows:

然后发布更新如下:

$.post("UpdateContact.aspx",
        dataString,
        afterUpdate,
        "json");

helper functions

辅助函数

function afterUpdate(data){
//add some post-update info
}

function getDataString(divId, tagName) {
    var data = "";
    var elements = $("#" + divId + " " + tagName);
    for (var i = 0; i < elements.length; i++) {
        var el = elements[i];
        var name = el.name;
        var value = $(el).val();
        if (value != null && value != "undefined")
            value = $.trim(value + ""); //added "" to fix IE 6 bug for empty select     
        if (el.type == "checkbox")
            value = el.checked;
        else if (el.type == "radio" && !el.checked)
            value = "";
        if (!(value == "" || value == "undefined" || name == "" || name == "undefined"))
            data += "&" + name + "=" + escape(value);
    }

    return data;
}