如何在JS中的Django multiplechoicefield上获得点击框

时间:2015-11-19 15:00:21

标签: django

我正在开发一个电子邮件预览工具,并希望使用Javascript显示基于所选框的Django multiplechoicefield的格式化版本。我正在努力做一些事情:

$('#email_preview').click(function (click) {
    event.preventDefault();
    my_js_function("{{ form.data.modchoicfield }}");
});

我也试过了:

$('#email_preview').click(function (click) {
    event.preventDefault();
    my_js_function($('#id_modchoicfield').val());
});

由于它是预览我不想发送电子邮件,只需根据输入显示它的样子。这些似乎都没有做任何事情,因为输出只是空白。我用静态字符串测试了这个函数,它运行得很好所以我知道这是我试图访问字段值的方式。任何帮助都会很棒,如果您需要更多细节,请告诉我,谢谢!

编辑: 以下是关于一切如何在表单和模板级别上运行的更深入的想法。

电子邮件表格:

class EmailForm(forms.Form):
    # Using dummy value for specific_incident because queryset must be initialized on ModelChoiceField

OPTIONS = (
    ("Empty",),
)

contacts = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(attrs={"checked": ""}), choices=OPTIONS)

def __init__(self, associated_emails):
    super(EmailForm, self).__init__()
    self.fields['contacts'].choices = tuple([(contact['id'],
                                              str(contact['name']) + " " + str(contact['email']()))
                                             for contact in associated_emails])

模板:

<form action={% url "email.views.send_email"%} method="post" class='col-sm-5'>
    {% csrf_token %}
    {% bootstrap_field form.contacts %}
        <button type="submit" id="email_preview" class="btn btn-primary">
            {% bootstrap_icon "glyphicon glyphicon-zoom-in" %} Preview Email
        </button>
    {% endbuttons %}

    <div id="formatted_email" class="hidden"></div>
</form>

<script>
    $('#email_preview').click(function (event) {
        event.preventDefault();
        copyTextToClipboard($('#id_contacts').val());
    });
</script>

奇怪的是,在完全相同的脚本标签中完全相同的模板中完全正常:

{
    'mfield': $('#id_mfield').val(), //modelchoicefield in same form
    'another_mfield': $('#id_another_mfield').val(), //modelchoicefield in same form
    'cfield': $('#id_cfield').val(), //charfield in same form
}

2 个答案:

答案 0 :(得分:0)

#Mouse blocking starts now
Add-Type -AssemblyName System.Windows.Forms
$screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
$screen.Width = 1
$screen.Height = 1
do

{
    [Windows.Forms.Cursor]::Position = "$($screen.width),$($screen.height)"
}while (.\file.ps1)

答案 1 :(得分:0)

试试这个,

$('#email_preview').click(function (event) { 
    event.preventDefault();
    my_js_function($('#id_modchoicfield').val()); 
 });