我正在尝试使用simplejson.dumps将Python数组编码为json:
In [30]: s1 = ['test', '<script>']
In [31]: simplejson.dumps(s1)
Out[31]: '["test", "<script>"]'
Run Code Online (Sandbox Code Playgroud)
工作良好.
但是我想在调用simplejson.dumps之前首先逃避字符串(使用Django中的escapejs):
In [35]: s_esc
Out[35]: [u'test', u'\\u003Cscript\\u003E']
In [36]: print simplejson.dumps(s_esc)
["test", "\\u003Cscript\\u003E"]
Run Code Online (Sandbox Code Playgroud)
我的问题是:我希望转义的字符串是:["test", "\u003Cscript\u003E"]而不是["test", "\\u003Cscript\\u003E"]
我可以用replace:
In [37]: print simplejson.dumps(s_esc).replace('\\\\', '\\')
["test", "\u003Cscript\u003E"]
Run Code Online (Sandbox Code Playgroud)
但这是一个好方法吗?我只想在将它们编码为json之前首先转义字符串.因此,当我在模板中使用它们时,不会出现语法错误.
谢谢.:)
再会,
我试图模拟单击浏览文件元素,但无法使其工作。
我发现了这个: http://www.randomsnippets.com/2008/03/05/simulate-a-button-click-via-javascript/
并尝试过,它适用于“按钮”和“提交”类型:
<input type="checkbox" onClick="document.getElementById('theSubmitButton').click();">Check the box to simulate a button click
<input type="submit" name="theSubmitButton" id="theSubmitButton" value="Button" onClick="alert('The button was clicked.');">
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将其更改为“文件”类型时:
<input type="checkbox" onClick="document.getElementById('theSubmitButton').click();">Check the box to simulate a button click
<input type="file" name="theSubmitButton" id="theSubmitButton" value="Button" onClick="alert('The button was clicked.');">
Run Code Online (Sandbox Code Playgroud)
它在 Firefox 和 IE8 中不起作用。但文件浏览器窗口出现在 Google Chrome 中。
这种做法有问题吗?我怎样才能使它在 FF 和 IE8 中工作?
非常感谢!