如何将一个onChange事件添加到Django表单字段中,并将字段本身作为参数

如何将一个onChange事件添加到Django表单字段中,并将字段本身作为参数

问题描述:

我正在动态地在TextField上添加 onChange 事件。以下代码解释了我在做什么:

I'm adding onChange event on a TextField dynamically. The following code explains what I'm doing:

fields.widget.attrs['onchange'] = 'execute_function(arg)'

我已将字段本身添加为javascript函数的参数,类似于 function_name (这)在django表格中。

I have add field itself as an argument to javascript function similar to function_name(this) in django forms.

我想将字段本身传递给execute_function()作为函数的参数。

I want to pass the field itself to execute_function() as an argument to the function.

如果我理解你的问题,应该像将 arg 更改为此:

If I understand your question it should be as easy as changing arg to this:

fields.widget.attrs['onchange'] = 'execute_function(this)'