jQuery使用textarea进行序列化错误 [英] jQuery serialize error with textarea filed

查看:193
本文介绍了jQuery使用textarea进行序列化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此功能在后台提交带有自定义消息的表单.除textarea字段外,它运行完美.我已经读到序列化函数在ex上有问题.换行符.

I'm using this function, to submit form in the background, with custom messages. It works perfectly, except with textarea fields. I've read that the serialize function has problems with ex. linebreaks.

 $(function() {
      $("#comment_form").validate({    submitHandler: function(form) {
        $.post('/u/r/l/', $("#comment_form").serialize(),
 function(data) {
            $('#comment_container').html(data);
                });
            }
        });

textarea是一个标记!编辑器区域.

The textarea is a markitup! editor area.

推荐答案

如此处所述: http ://api.jquery.com/serialize/#comment-67394779

function keepLB (str) { 
  var reg=new RegExp("(%0A)", "g");
  return str.replace(reg,"%0D$1");
}

$(function() {
  $("#comment_form").validate({ submitHandler: function(form) {
    $.post('/u/r/l/', keepLB($("#comment_form").formSerialize()), function(data) {
      $('#comment_container').html(data);
    });
  }
});

如果它不起作用,请手动对textarea数据进行urlencode:

If it doesn't work, manually urlencode the textarea data:

$(function() {
  $("#comment_form").validate({ submitHandler: function(form) {
    $.post('/u/r/l/', "textareadata="+escape($("#mytextarea").value), function(data) {
      $('#comment_container').html(data);
    });
  }
});

如果您还想发送其他表单内容(请注意:在此处不要给textarea一个名称",只需一个id!):

And if you also want to send other form contents (note: don't give the textarea a "name" here, just an id!):

$(function() {
  $("#comment_form").validate({ submitHandler: function(form) {
    $.post('/u/r/l/',
    $("#comment_form").formSerialize()+"&textareadata="+escape($("#mytextarea").value),
    function(data) {
      $('#comment_container').html(data);
    });
  }
});

这篇关于jQuery使用textarea进行序列化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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