简体   繁体   English

在webpy中,如何将javascript值分配给python变量?

[英]In webpy, how can I assign a javascript value to a python variable?

I'm looking for a simple way to assign a value held by a javascript variable to a python variable in a webpy template. 我正在寻找一种简单的方法来将javascript变量所保存的值分配给webpy模板中的python变量。 I have an int value held by a js variable that I want to use to get an element of a python array. 我有一个js变量持有的int值,我想使用它获取python数组的元素。 For example (if I want $list[0] in a template): 例如(如果我想要$ list [0]在模板中):

<script>
...
foo = 0
$i = foo        ??? (doesn't work...)
return $list[ foo ]    ??? (doesn't work...)
...
</script>

Sorry if that isn't as clear as I hope it is. 抱歉,如果我不希望那样清楚。 I've tried a ton of different ways to do this, and I can't seem to make it work. 我尝试了许多不同的方法来执行此操作,但似乎无法使其正常工作。 Thanks!!! 谢谢!!!

To expand on Pointy's answer, what you are asking for does not make sense because web pages code is run in a certain order, and you are trying to make it run backwards. 要扩展Pointy的答案,您要的内容没有任何意义,因为网页代码是按一定顺序运行的,而您正试图使其反向运行。

  1. The webserver (python) gets a request. 网络服务器(python)得到一个请求。 It uses templates to construct a response for that request, and returns and HTML page and sends it to the client. 它使用模板构造对该请求的响应,并返回HTML页面并将其发送到客户端。 At this point, generally, the python code is done running. 至此,一般而言,Python代码已完成运行。

  2. The client (browser) gets the web page. 客户端(浏览器)获取网页。 It loads all the other resources it needs to display it, and then runs the javascript. 它加载显示它所需的所有其他资源,然后运行javascript。

The javascript and python, therefore, are running on different computers at different times. 因此,javascript和python在不同的时间在不同的计算机上运行。 The solution to your problem is probably going to be a little more complicated then you were hoping for: you have to make a server request from javascript to get the data from the server after the page is loaded on the client, and then manually add it to the DOM . 解决您问题的方法可能会比您希望的要复杂一些:在页面加载到客户端后,您必须从javascript发出服务器请求以从服务器获取数据,然后手动添加到DOM

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

相关问题 如何为会话值分配JavaScript变量 - How can I assign a javascript variable to the session value 如何将python变量的值(字符串)分配为JavaScript变量中的值 - How to assign a python variable's value(string) as the value in JavaScript variable 仅使用JavaScript,如何查询Google工作表并将单个单元格的值分配为JavaScript变量? - Using JavaScript only, how can I query a google sheet and assign the value of a single cell as a JavaScript variable? 我如何将 php 变量的值分配给 javascript 变量 - how do i assign value of php variable to javascript variable 每次我将变量分配给 javascript 中的不同值时,如何将变量传递给 function - How can I pass a variable to a function every time I assign it to a different value in javascript 我可以在Javascript中一次为多个属性分配变量值吗? - Can I assign a variable value to multiple attributes at once in Javascript? 如何将javascript函数的返回值分配给python中的变量? - how to assign a javascript function return value to the variable in python? 我们如何将数组的单个值分配给javascript中的任何变量 - how we can assign single value of array to any variable in javascript 如何在JavaScript中将PHP文件名分配为变量值? - How do I assign a PHP file name as a variable value in JavaScript? 如何将PHP变量值分配给JavaScript变量 - How to assign a PHP variable value to JavaScript variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM