2011-03-20 27 views
1

我想使用JavaScript变量导入python Block。使用javascript变量导入Python Block

<script> 
$(document).ready(function() { 
    $("#WO_cpp_id").change(function() { 
    id = this.selectedIndex; 
    ajax('{{=URL(r=request,f='get_CIs',vars={'CPP_Id':'#here I want to use id variable')}}', ['WO_cpp_id'], 'WO_ci_id'); 
}) 
.change(); }); </script> 

由于提前

回答

0

这是行不通的。 Python在客户端上呈现页面之前在服务器上运行;在页面呈现后,Javascript在浏览器中运行。当Python代码运行时,甚至没有设置id变量。

相反,您应该让您的javascript代码添加要设置为现有查询字符串的额外数据(或使用jQuery的ajax选项的data属性)。

3

您的python代码正在服务器上运行。您的JavaScript代码(如引用的)正在客户端上运行。所以你不能在你的Python代码中直接使用JavaScript变量。你所做的是发送你想从客户端以任何方式发送到服务器的数据。

其中的一种方法是“阿贾克斯”。此客户端代码将变量foo的内容发送到服务器上张贴“fooParameter”参数:

jQuery docsthe Wikipedia article on ajax
var foo = "This is some information"; 
$.ajax({ 
    url: "myscript.py", 
    method: "POST", 
    data: {fooParameter: foo}, 
    success: function(responseData) { 
     // Successful POST; do something with the response if you want 
    }, 
    error: function(jxhr, statusText, err) { 
     // Error, handle it 
    } 
}); 

更多。