以下代码返回'undefined'...
$('select').change(function(){
alert($(this).data('id'));
});
<select>
<option data-id="1">one</option>
<option data-id="2">two</option>
<option data-id="3">three</option>
</select>
Run Code Online (Sandbox Code Playgroud) 似乎应该已经被问过几百次(双关语很有趣),但我只能找到圆形浮子的功能.如何舍入整数,例如:130 -> 200?
我正在尝试解析来自youtube api的json响应数据,但我一直收到错误.
这是窒息的片段:
data = json.loads("""{ "entry":{ "etag":"W/\"A0UGRK47eCp7I9B9WiRrYU0.\"" } }""")
Run Code Online (Sandbox Code Playgroud)
..这发生了:
JSONDecodeError: Expecting , delimiter: line 1 column 23 (char 23)
Run Code Online (Sandbox Code Playgroud)
我已经确认它是有效的json,我无法控制它的格式,所以我怎么能通过这个错误?
使用正则表达式,如何用逗号(,)替换每个换行符char(\n)?
像这样:
Demetrius Navarro
Tony Plana
Samuel L. Jackson
Run Code Online (Sandbox Code Playgroud)
至:
Demetrius Navarro,Tony Plana,Samuel L. Jackson
Run Code Online (Sandbox Code Playgroud)
不是在特定的编程语言中,只是标准的正则表达式.像这样的东西:
(.*)
$1
//This just takes the whole string and outputs it as is, I think
Run Code Online (Sandbox Code Playgroud) 可能重复:
Python:按值对字典排序
d = {"a":4, "b":12, "c":2}
Run Code Online (Sandbox Code Playgroud)
如果我使用sorted()与lambda:
s = sorted(d.items(), key=lambda(k,v):(v,k))
Run Code Online (Sandbox Code Playgroud)
我得到一个列表元组(键,值)但我又想要一个字典:
{"c":2, "a":4, "b":12}
Run Code Online (Sandbox Code Playgroud)
而dict(the_list_of_tuples)你正在回到原点.
我使用带有webapp2的Jinja2.
正如他们的文档所说,Jinja2将所有"上下文"数据编码为unicode.当我尝试将json字符串插入模板时,这证明是有问题的:
jsonData = json.loads(get_the_file('catsJson.txt'))
Run Code Online (Sandbox Code Playgroud)
我将jsonData传递给模板,我能够成功循环它但是当我将一个json元素插入HTML时,它看起来像这样:
<option value='[u'dogs', u'cats']'>
Run Code Online (Sandbox Code Playgroud)
我希望它看起来像这样(因为它在原始的json字符串中):
<option value='["dogs", "cats"]'>
Run Code Online (Sandbox Code Playgroud)
有什么建议?
如果你使用Django或Jinja2,你可能以前遇到过这个问题.我有一个JSON字符串,如下所示:
{
"data":{
"name":"parent",
"children":[
{
"name":"child_a",
"fav_colors":[
"blue",
"red"
]
},
{
"name":"child_b",
"fav_colors":[
"yellow",
"pink"
]
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想将它传递给我的Jinja2模板:
j = json.loads('<the above json here>')
self.render_response('my_template.html', j)
Run Code Online (Sandbox Code Playgroud)
...并像这样迭代:
<select>
{% for p in data recursive %}
<option disabled>{{ p.name }}</option>
{% for c in p.children %}
<option value="{{ c.fav_colors|safe }}">{{ c.name }}</option>
{% endfor %}
{% endfor %}
</select>
Run Code Online (Sandbox Code Playgroud)
这就是我遇到问题的地方:除了Jinja2输出c.fav_colors的unicode编码值之外,一切正常.我需要c.fav_colors作为有效的javascript数组,以便我可以从javascript访问它.我怎样才能让Jinja将该值打印为ascii文本,['blue','red']而不是[u'blue', u'red']?
我有下一个链接,允许用户浏览URL列表(存储在数组中).当用户单击下一步时,我希望为我的阵列中的下一个网站更新链接href和锚点.到目前为止我尝试过的方式,它每次链接跳过一个URL,我知道为什么,它与之前的点击有关甚至没有完成,但是这样做的正确方法是什么?
这是一个例子,所以你可以看到我正在谈论的内容:http://jsbin.com/atobep
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
var items = [];
$.each(urls, function(i, item) {
items.push("<li>"+item['name']+"</li>");
});
$('#list').append(items.join(''));
changeNextLink(0);
$("#frame").attr('src', urls[0]['url']);
});
var urls = [{"name":"ebay","url":"http://ebay.com"},{"name":"amazon","url":"http://amazon.com"},{"name":"msn","url":"http://msn.com"},{"name":"yahoo","url":"http://yahoo.com"},{"name":"wikipedia","url":"http://wikipedia.org"}];
function changeNextLink(x){
$('#now').html(urls[x]['name']); //show the name of currently loaded page
$('#nextLink').html(urls[x+1]['name']); //name of next website as anchor of next link
$('#nextLink').attr('href', urls[x+1]['url']); //url to next website
$('#nextLink').attr('onclick','changeNextLink('+(x+1)+')'); //the problem spot. prepare next link for next click.
}
</script>
</head>
<body>
<ol id="list"></ol> …Run Code Online (Sandbox Code Playgroud) 使用CustomSearchControl时,结果页面的数量会根据您正在查看的页面而有所不同.例如,访问此站点并搜索:car
在第一个结果页面上,它将显示指向3个页面的链接,但是当您单击2或3时,它将在第2页结束.
为什么会发生这种情况,是否有解决方法?
如果有可能得到实际的结果数,并建立我自己的分页?
我有一个函数搜索列表列表中的字符串然后返回包含匹配列表的列表:
def foo(myList,keyword,first=True):
if first: #Search only first element or each sublist
return [x for x in myList if keyword in x]
else: #Search first and second elements of each sublist
return [x for x in myList if keyword in x or keyword in x[1]]
Run Code Online (Sandbox Code Playgroud)
现在我想扩展它来处理高级搜索,例如:
matchthis -butnothis -"and not this"
this|orthis|"or this"
brand new*laptop # this is a wildcard, matches like: brand new dell laptop
"exact phrase"
Run Code Online (Sandbox Code Playgroud)
是否有我可以在我的函数中使用的python模块(最好是内置的)来处理这些查询?
PS:我知道Swoosh,但它现在不适合我.另外,我目前正在使用App Engine.
我正在尝试做的基本上是内存中的全文搜索,因为app引擎还不支持全文搜索.我查询数据存储区,将实体放入列表并循环遍历这些列表以查找查询匹配项.