使用JQuery执行Ajax Post时输入值 [英] enter value when doing Ajax Post with JQuery

查看:104
本文介绍了使用JQuery执行Ajax Post时输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用"div内容可编辑" html从输入键盘获取值,以使用ajax jquery发布并发送到数据库. 这是我的html代码

i'm using "div content editable" html to get value from input keyboard to post with ajax jquery and send to database. this is my html code

<div class="form_field" name="contentbox" id="contentbox" contenteditable="true"> 

和我的JavaScript

and my javascript

function mysubmit {
    var contentbox = $("#contentbox").html();
    var contentboxvalue = "contentboxvalue ='" + escape(contentbox) + "'";
    $.ajax({
        type: "POST",
        url: "<?php echo base_url() ?>admin/data",
        data: contentboxvalue,
        cache: true,
        success: function () {
            document.getElementById("contentboxInfo").innerHTML = contentbox;
        }
    });

}

但是在更新数据库中的值时出现问题,例如:"hello world" 当我更新时,ajax post jquery发送值:

But I have a problem when I'm updating the value in my database, example: "hello world" and when I'm updating, ajax post jquery send value:

"
hello world"

提交给数据库时如何删除换行符(CR/LF),但不删除<br>标签.

How to remove linebreaks (CR/LF), but not <br> tags when submitting to database.

推荐答案

您可以使用$.trim()删除字符串中的多余空格.

You can use $.trim() to remove extra white-space in a string.

$.trim()函数删除所有换行符,空格(包括 (不间断空格),以及制表符开头和结尾处的制表符 提供的字符串.如果这些空白字符出现在 字符串,它们会保留下来.

The $.trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string. If these whitespace characters occur in the middle of the string, they are preserved.

来源: http://api.jquery.com/jquery.trim

例如.

function mysubmit{
    var contentbox      = $("#contentbox").html(),
        contentboxvalue = "contentboxvalue ='" + escape($.trim(contentbox)) + "'";
    $.ajax({
        type    :"POST",
        url     : "<?php echo base_url() ?>admin/data",
        data    : contentboxvalue,
        cache   : true,
        success : function() {
            document.getElementById("contentboxInfo").innerHTML = contentbox;
        }
    });
}

这篇关于使用JQuery执行Ajax Post时输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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