我需要编码一些字符串来添加到URL.
我的字符串包含多个单词:( "abc def hij"即不是三个单独的字符串,而是一个单独的字符串,其中包含3个单词).
在JavaScript或jQuery的,你怎么转换那样的字符串("abc def hij")到编码格式:"abc+def+hij"?
如果我循环来自JSON调用的结果,我怎么知道我是否在第一次迭代?
在这个例子中,我编写了loopIndex变量来表示我正在寻找的东西 - 但它实际上并没有在任何地方设置.
你怎么知道你在循环中的位置 - 你在哪个迭代?
$.getJSON(myurl, function(data) {
$.each(data.results, function() {
// what's the index of this iteration?
if(loopIndex==0){
console.log("first iteration!");
}
});
});
Run Code Online (Sandbox Code Playgroud) data.results在尝试对其执行操作之前,如何检查以下内容是否为空?
$.getJSON(myurl, function(data) {
// if data.results is not empty
// {
// console.log(data.results.size);
// }
});
Run Code Online (Sandbox Code Playgroud)