Javascript jQuery在搜索为空时传递静态值

Javascript jQuery在搜索为空时传递静态值,javascript,jquery,Javascript,Jquery,我正在使用$('input[type=“text”]”)传递搜索字符串。val() 现在的要求是: 如果是空白搜索,则我希望将空白搜索作为值传递,而不是$('input[type=“text”]”)。val() 及 如果它不是空的并且有值,我希望使用$('input[type=“text”]').val()传递相同的值 我所尝试的: $(document).on('click','.fa.fa-search', function() { ga('create', {{ GA Proper

我正在使用
$('input[type=“text”]”)传递搜索字符串。val()

现在的要求是:
如果是空白搜索,则我希望将空白搜索作为值传递,而不是
$('input[type=“text”]”)。val()

如果它不是空的并且有值,我希望使用
$('input[type=“text”]').val()传递相同的值

我所尝试的:

$(document).on('click','.fa.fa-search', function()
{
    ga('create', {{ GA Property }}, 'auto'); 
    ga('send', 'event', 'Search', 
    function blank () 
    { 
      alert('hi');
        var srchStr = $('input[type="text"]').val(); 
        if(srchStr == '') { srchStr = "Blank Search"; }
    }, 
    window.location.href); 
}); 
怎么做?

试试这个:

var srchStr = $('input[type="text"]').val();
if(srchStr == '')
{
    srchStr = "Blank Search";
}

请参见此示例,避免使用额外的空格

var v = $('input[type="text"]').val().replace(/\s+/,"");
v = v === "" ? "Blank Search" : $('input[type="text"]').val();

html:

最简单的方法:

if($('input[type="text"]').val() == '')
{
    $('input[type="text"]').val("Blank Search");
}
试试这个: 变量输入=$('input[type=“text”]”); var srchStr=input.val()

您可以使用三元
($('input[type=“text”]”).val().trim().length>0)$('input[type=“text”]').val():“Blank Search”
$(文档)。在('click','.fa.fa Search',function(){ga('create',{ga Property}},'auto');ga('send','event','Search',function Blank(){var srchStr var=$('input[type=“text”]').val();if(srchStr='',{srchStr Blank Search;},window location.href);})这是我的原始代码,我有。。如果搜索为空,则我希望值为“空白搜索”,如果不为空,则我希望搜索值/字符串。
$('input[type="button"]').on("click", function() {
  var val = $('input[type="text"]').val().trim().length > 0 ? $('input[type="text"]').val() : "Your Default value"
  alert(val);
});
if($('input[type="text"]').val() == '')
{
    $('input[type="text"]').val("Blank Search");
}
if(srchStr != '' && srchStr.trim().length > 0){
  input.val(srchStr);
}else{
  input.val("Blank Search");
}