jquery S.get sending data with trim()
How to send triimed data using $.get() see code snippet below:
$("#txtSearch").keyup(function() {
$.get("/controller/method/",
{ 'param1': $.trim($("#txtSearch").val()) !== "" ? $.trim($("#txtSearch").val()) : return false; },
function(data) {
$("#div").html(data);
});
});
This results in syntax error whether or not i give semi colon(":") after return false. So i just want to ask should i use var & assign txtBox data to var & then serach if yes then how to use var after giving 1st parameter i.e开发者_运维问答 url path in $.get as i am using it inside of keyup function so i have to trim data inside of $.get.
Call $.get
after checking the value of the textbox.
$("#txtSearch").keyup(function() {
var textVal = $.trim($("#txtSearch").val();
if ( textVal !== "" )
{
// call your $.get function here.
}
else
{
return false;
}
});
I use this small utility function:
jQuery.fn.trimAndSerialise = function ()
{
this.find('input, textarea').each(function()
{
$(this).val(jQuery.trim($(this).val()));
});
return this.serialize();
}
Then use it like this:
var f = $('#aForm');
var data = f.trimAndSerialise();
P.S. I haven't tested this with $.get though (I normally use $.ajax...)
This seems the most sensible:
$("#txtSearch").keyup(function() {
var strSearchVal = $.trim($("#txtSearch").val());
if (strSearchVal !== "")
{
$.get("/controller/method/",
{ 'param1': strSearchVal},
function(data) {
$("#div").html(data);
});
}
else
{
return false;
}
});
精彩评论