jQuery——是否需要URL对变量进行编码?

时间:2022-05-11 15:21:41

I am using ColdFusion 9 and the latest and greatest jQuery.

我正在使用ColdFusion 9和最新最好的jQuery。

At the top of my page, I use this:

在我的页面顶部,我用了这个:

<cfajaxproxy cfc="artists" jsclassname="jsApp">

I have a search field:

我有一个搜索域:

<input id="Artist" class="Search" type="text">

When a user types in the search field, the value is passed into a jQuery function:

当用户在搜索字段中输入时,将值传递给jQuery函数:

$(".Search").keyup(function() {
  var Artist = $("#Artist").val();
  var QString = "Artist=" + Artist;
  $("#ArtistSearchResultsDiv").load("ArtistSearchResults.cfm?"+QString);
});

The search results div loads a page with these items in CFSCRIPT:

搜索结果div在CFSCRIPT中加载了一个页面:

objArtists = createObject("component", "artists");
GetArtists = objArtists.getArtists(Artist);

I have a CFC that runs the query and returns the correct records.

我有一个CFC,它运行查询并返回正确的记录。

The PROBLEM is that when I type in the search box, as soon as I hit a space, no further value is added to the QString variable, and so those values aren't passed to the query.

问题是,当我在搜索框中输入时,一旦我点击空格,就不会向QString变量添加任何值,因此这些值不会传递给查询。

Here's how much search string looks in Firebug when searching for "The Beatles":

以下是在Firebug中搜索“披头士”时搜索字符串的效果:

GET http://127.0.0.1:8500/WebSites/AwesomeAlbums/GlobalAdmin/ArtistSearchResults.cfm?Artist=The

It's stops as soon as it sees a space.

它一看到空间就停下来。

So, if you were searching for "The Beatles", only the value "The" would be passed into the QString variable. If you were searching for "Celine Dion", only "Celine" would be passed.

因此,如果您正在搜索“披头士”,那么只有“The”的值会被传递到QString变量中。如果你在寻找“席琳迪翁”,只有“席琳迪翁”会被淘汰。

I am assuming that I need to URL encode the QString somehow. Is that correct? How do I do that?

我假设我需要以某种方式URL编码QString。那是正确的吗?我该怎么做呢?

1 个解决方案

#1


9  

var QString = "Artist=" + encodeURIComponent(Artist);

#1


9  

var QString = "Artist=" + encodeURIComponent(Artist);