我有一个带有表单的html。
用户提交表单后,表单中的值将使用ajax发送到数据库。
我的问题是用户提交的字段之一中带有加号(+)的窗体,加号不会显示在数据库中。
我的代码:
function update()
{
var branch_id = 1;
var saleTitle = $("#title").val();
var saleText = $("#text").val();
var imgSrc = $("#imgSrc").html();
var datastr ='branch_id=' + branch_id + '&saleTitle=' + saleTitle +
'&saleText=' + saleText + '&imgSrc=' + imgSrc + '&func=update';
$.ajax({
type: "POST",
url: "update.php",
data: datastr,
success: function(msg){
//alert( "Data Saved: " + msg );
if (msg == "")
{
$("#message").html("Update was successful!");
}
else
{
$("#message").html("Error") + " " + msg;
}
}
});
}
最佳答案
传递对象,而不是字符串。
var dataobj = {
branch_id : branch_id,
saleTitle : saleTitle,
saleText : saleText,
imgSrc : imgSrc,
func : "update"
};
// …
data: dataobj
然后jQuery将负责转义数据(
+
表示此数据格式中的空格),然后将其连接到application / x-www-form-urlencoded数据中。关于javascript - Javascript特殊字符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8712848/