javascript 如何将查询参数作为字符串传递到包含空格的 jquery 负载中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14757691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:30:19  来源:igfitidea点击:

How to pass query parameter as string into jquery load that includes space

javascriptjqueryquery-stringmultiline

提问by Chun ping Wang

Hi i have a simple question. I want to know how to pass a query string into a query parameter using jquery.

嗨,我有一个简单的问题。我想知道如何使用 jquery 将查询字符串传递给查询参数。

function loadPage(queryString) {
    jQuery("#divId").load("myurl/action?param=" + queryString);
}

queryString could be like "1,2,3,4" or "testing 1 2 3".

queryString 可能类似于“1,2,3,4”或“testing 1 2 3”。

When I tried it only the first parameter is their. I want to be able to pass in a sentence, or a paragraph.

当我尝试它时,只有第一个参数是他们的。我希望能够传入一个句子或一个段落。

回答by marty

try

尝试

function loadPage(queryString) {
    jQuery("#divId").load("myurl/action?param=" + encodeURIComponent(queryString));
}

回答by Chun ping Wang

Use encodeURIComponent():

使用encodeURIComponent()

function loadPage(queryString) {
    jQuery("#divId").load("myurl/action?param=" + encodeURIComponent(queryString));
}