replacing a input value
Is there a better way to replace a string as having & in the textarea breaks the script as it thinks its another & parameter?开发者_StackOverflow社区
function doRequest(){
var str = $('textarea#tr').val();
$.ajax({
type: "POST",
url: "index.php?sd=t",
data: "txt="+str.replace(/&/g, "and"),
success: function(){
$("div").css('color','red');
$("div").text('Saved');
$("div").fadeTo(800,1);
$("div").animate({backgroundColor:'#000000'}, 200);
$("div").animate({backgroundColor:'#FFFF90'}, 400);
stre = false;
}
});
}
You can pass your data as an object and let jQuery serialize it, like this:
data: {txt: str},
Note this won't put the word "and", it'll escape it, leaving %26
.
What actually happens under the covers is calling encodeURIComponent()
, like this:
data: "txt="+encodeURIComponent(str),
I'd go with the first method, I'm just showing what is happening underneath for better understanding of how the encoding works.
data: { txt: $('textarea#tr').val() }
精彩评论