Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

jquery - Insert a var into a string

RESOLVED It was determined that the issue is only on the platform we use thus all answers provided could be probably be right.

This question is based on a previous one


This code of tab selections:

<div class="searchTabContainer col-xs-7" id="searchTabContainer">
    <div class="searchTab" id="searchTab1" onclick="javascript:return setActiveTab(1);">MAKE 1</div>
    <div class="searchTab" id="searchTab2" onclick="javascript:return setActiveTab(2);">MAKE 2</div>
    <div class="searchTab" id="searchTab3" onclick="javascript:return setActiveTab(3);">MAKE 3</div>
    <div class="searchTab" id="searchTab4" onclick="javascript:return setActiveTab(4);">MAKE 4</div>
    <div class="searchTab" id="searchTab5" onclick="javascript:return setActiveTab(5);">MAKE 5</div>
</div>

Assign classes and create a new var:

$(".searchTab").removeClass("active");
    $(".Filter").find("input[type=text], textarea, select").val("");
    $("#searchTab" + x).addClass('active');
    if (x == 0){
            searchBrand = ""
    }
    else if (x == 1) {
            searchBrand = "make1"
    }
    else if (x == 2) {
            searchBrand = "make2"
    }
    else if (x == 3) {
            searchBrand = "make3"
    }
    else if (x == 4) {
            searchBrand = "make4"
    }
    else if (x == 5) {
            searchBrand = "make5"
    }

Combined with a homepage link:

<p>
  <a href="/search?q=body%20mirrors&type=product">Mirrors</a>
</p>
<p>
  <a href="/search?q=body%20air&type=product">Air</a>
</p>
<p>
  <a href="/search?q=body%20hydraulics&type=product">Hydraulics</a>
</p>

Gets edited to a new url for searching:

$('.homePageProducts a').each(function(){
  var newurl = $(this).attr('href').replace('/search?q=', '/search?q=' + searchBrand + '%20');
  $(this).attr('href', newurl);
});

This process works good but when to makes is click subsequently before the final line, both appear in the URL like:

https://www.example.com/search?q=make3%20make5%20%20body%20air&type=product

The default homepage search result without make results to:

https://www.example.com/search?q=%20body%20air&type=product

The question before provided a possible solution to regex but it would also appear that of instead of replacing the homepage url, only insert the searchBrand into the default search url to achieve:

https://www.example.com/search?q=make3%20body%20air&type=product

This might resolve the multiple makes issue. I have tried:

var newurl = $(this).attr('href').insert(10, searchBrand + '%20');

and a few variations of this without success of getting the searchBrand inserted into newurl.
How can I achieve this result?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe trying simple JavaScript could be the trick. Using a new:

var url = "Your first part of the link goes here" + searchBrand + "end of your link"; 

Then just call the var when you need it. Hope that helps!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...