简体   繁体   English

我可以将html元素传递到jinja变量中吗?

[英]Can I pass html elements into a jinja variable?

I am trying to set up a way to automatically format an input field to use the currency function of python's locale module. 我试图建立一种自动格式化输入字段以使用python语言环境模块的货币功能的方法。 However, I am running into an issue passing references into segment of jinja. 但是,我遇到了一个将引用传递到jinja段的问题。

If I hardcode the value of the input like this it gives me my correctly formatted value: 如果我像这样对输入的值进行硬编码,它将为我提供正确格式化的值:

<input onkeyup="this.value='{{ currency(20)) }}'>

However, what I really need is instead of passing in 20, I want it to use user input, therefore I need to pass in the current value of the input field itself. 但是,我真正需要的不是传递20,而是希望它使用用户输入,因此我需要传递输入字段本身的当前值。

I tried something like : 我尝试了类似的东西:

<input onkeyup="this.value='{{ currency(this.value())) }}'>

I have also tried using document.getElementbyId and a number of other variations, problem is that none of those objects are available inside of the {{ }}. 我也尝试过使用document.getElementbyId和许多其他变体,问题是{{}}内没有这些对象。 Anyone have any sugestions, is jinja capable of doing this type of dynamic processing? 任何人都有建议,神社有能力进行这种动态处理吗?

This isn't possible because Jinja creates the HTML before the page is sent to the client and any Javascript runs. 这是不可能的,因为Jinja会在将页面发送给客户端并运行任何Javascript之前创建HTML。

The only way to do this dynamically in serverside Python would be via an AJAX call which would be very inefficient. 在服务器端Python中动态执行此操作的唯一方法是通过AJAX调用,这将非常低效。

My suggestion is you find an implementation of the currency formatting function in Javascript. 我的建议是您在Javascript中找到货币格式化功能的实现。 A quick google on this should give you lots of options. 快速谷歌应该给你很多选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用 jQuery.html() 传递变量和一些 HTML - How can i pass a variable and some HTML using jQuery .html() 如何将 jinja 变量传递给 javascript - How to pass a jinja variable to javascript 如何将jinja2对象传递给jquery函数作为参数? - How can I pass a jinja2 object to a jquery function as parameter? 如何将 JS 输入中的变量传递给 HTML? - How can I pass a variable from a JS input to HTML? 如何将变量从html传递给指令AngularJS? - How can I pass a variable from html to directive AngularJS? 如何将 JavaScript 变量传递给 Jinja 标签中的函数 - How to pass JavaScript variable to function in Jinja tag 我可以将URL作为变量传递吗? - Can I pass a URL as a variable? 如何在jQuery中获取具有特定父类的所有元素的内容并将它们传递给GET变量? - How can I get the contents of all elements with a specific parent class in jQuery and pass them in a GET variable? 使用动态创建的dom元素和一个我似乎无法传递的变量来创建软件合成器 - Creating a software synth with dynamically created dom elements and a variable that I can't seem to pass 我如何才能打开一个新窗口并将其仅传递给带有Javascript的选定html元素(以供以后打印它们使用)? - How can I open a new window and pass to it (for the later purpose of printing them) only selected html elements with Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM