Django表单字段内的JavaScript函数调用 [英] JavaScript function calls within Django form fields

查看:348
本文介绍了Django表单字段内的JavaScript函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个表单,我有复选框,点击后,他们打开单独的文本区域,供用户输入更多信息。



如果我想要使用Django动态创建的表单域,是否有一种方法可以为每个复选框调用一个函数。

解决方案

您可以使用JavaScript动态添加事件处理程序。您可以添加一个脚本,一旦页面加载,将找到所需的所有复选框,并在其中添加处理程序。在 jQuery 中,您可以这样写:



<$ p $ ($($)$($)$($)$($)$($)$($)
});
});

小心,我还没有测试上面的代码!但应该足以让你开始。


I'd like to create a form where I have checkboxes, and when clicked, they open separate textareas for the user to enter more information in.

If I want to use Django's dynamically created form fields, is there a way that I can put a function call in for each checkbox.

解决方案

You can dynamically add event handlers using JavaScript. You can add a script that, once the page is loaded, will find all checkboxes you want and add the handlers there. In jQuery, you can write something like this:

$(document).ready(function() {
    $(".my_form input[type=checkbox]").change(function() {
          //Some code here
    });
});

Be careful, I have not tested the code above! But should be enough to get you started.

这篇关于Django表单字段内的JavaScript函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆