0

我正在使用 python 动态创建一个 jQuery 函数:

jQuery = ("$('%(other_id)').click(function() { "
              "    if ($(this).is(':checked')) { "
              "        $('%(text_id)').show()     "
              "    } "
              "    else {"
              "       $('%(text_id)').hide()"
              "    }"
              " });")

other_id我必须在and中插入变量text_id。我看到这个$符号用于字符串模板(虽然不知道它是做什么的)所以我用 double $-s( $$)转义它们

jQuery = ("$$('%(other_id)').click(function() { "
              "    if ($$(this).is(':checked')) { "
              "        $$('%(text_id)').show()     "
              "    } "
              "    else {"
              "       $$('%(text_id)').hide()"
              "    }"
              " });")

但是我仍然无法格式化:

>>> choice_id = 'foo'
>>> text_choice_id = 'bar'
>>> jQuery = ("$$('%(other_id)').click(function() { "
                  "    if ($$(this).is(':checked')) { "
                  "        $$('%(text_id)').show()     "
                  "    } "
                  "    else {"
                  "       $$('%(text_id)').hide()"
                  "    }"
                  " });")
>>> jQuery %{'other_id' : choice_id, 'text_id' : text_choice_id }

Traceback (most recent call last):
  File "<pyshell#123>", line 1, in <module>
    jQuery %{'other_id' : choice_id, 'text_id' : text_choice_id }
ValueError: unsupported format character ''' (0x27) at index 15

转义单引号后:

>>> jQuery = ("$$(\'%(other_id)\').click(function() { "
                  "    if ($$(this).is(\':checked\')) { "
                  "        $$(\'%(text_id)\').show()     "
                  "    } "
                  "    else {"
                  "       $$(\'%(text_id)\').hide()"
                  "    }"
                  " });")
>>> jQuery %{'other_id' : choice_id, 'text_id' : text_choice_id }

Traceback (most recent call last):
  File "<pyshell#125>", line 1, in <module>
    jQuery %{'other_id' : choice_id, 'text_id' : text_choice_id }
ValueError: unsupported format character ''' (0x27) at index 15

无法尝试string.format(),因为我在字符串中有括号。为什么我不断收到'一些不受支持的格式字符?

4

2 回答 2

6

您缺少格式化程序类型:

%(other_id)s

注意s括号后面的;您想将值插入为字符串。这是一个工作版本:

jQuery = ("$('#%(other_id)s').click(function() { "
                  "    if ($(this).is(':checked')) { "
                  "        $('#%(text_id)s').show()     "
                  "    } "
                  "    else {"
                  "       $('#%(text_id)s').hide()"
                  "    }"
                  " });")

美元符号在%-style 字符串格式中没有任何意义,我已经#为您添加了 id 选择器。:-)

就个人而言,我会改用"""三引号:

jQuery = """\
$('#%{other_id}s').click(function() {
    if ($(this).is(':checked')) {
        $('#%(text_id)s').show()
    }
    else {
        $('#%(text_id)s').hide()
    }
});
"""

更好的是,无论如何将其放入 Jinja 模板中(因为您使用的是 Flask)并渲染它:

jquery = render_template('toggle_field.js', other_id=choice_id, text_id=text_choice_id)

toggle_field.jsjQuery 片段的 Jinja 模板版本在哪里:

$('#{{ other_id }}').click(function() {
    if ($(this).is(':checked')) {
        $('#{{ text_id }}').show()
    }
    else {
        $('#{{ text_id }}').hide()
    }
});
于 2013-02-04T23:09:50.593 回答
0

考虑一种数据驱动的方法,而不是代码生成。静态定义以下两个函数,最好在所有 html 文件中包含的某个 js 文件中:

function toggle_if_checked(checkbox, toggleable) {
    var cbox = $(checkbox), tgl = $(toggleable);
    tgl.toggle(cbox.is(':checked'));
}

function register_check_show_events(elist) {
    var i, cboxselector, textselector;
    function handler(e) {
        toggle_if_checked(e.target, e.data);
    }
    for (i = 0; i < elist.length; i++) {
        cboxselector = '#'+elist[0];
        textselector = '#'+elist[1];
        $(cboxselector).on('click', null, textselector, handler);
    }
}

然后注册您的事件处理程序,收集一个 Python id 列表并通过 JSON 将其提供给 javascript。

import json

ids = [('cboxid1','textboxid1'),('cboxid2','textboxid2')]
json_ids = json.dumps(ids)
script = 'register_check_show_events({});'.format(json_ids)

一般来说,如果您只通过 JSON 在 Python 和 JS 层之间传递数据而不是动态生成 javascript 代码,那么您的代码将更干净且更易于维护。

于 2013-02-05T00:02:18.590 回答