执行按钮Python脚本点击

问题描述:

我有一个按钮,一个HTML页面,我需要执行一个Python脚本,当我们点击按钮,返回到相同的HTML页面的结果。执行按钮Python脚本点击

所以我需要对返回值做一些验证并执行一些操作。

这里是我的代码:

HTML:

<input type="text" name="name" id="name"> 
<button type="button" id="home" onclick="validate()" value="checkvalue"></button> 

JS:

function validate(){ 
    if (returnvalue=="test") alert(test) 
    else alert ("unsuccessful") 
} 

什么我的Python代码正在做的是对的名称一些验证在进入文本框并给出返回状态。

但我需要在同一页面上返回结果,所以我可以在稍后提交表单提交所有细节。任何帮助将不胜感激

+0

的Python运行在服务器端,这样你就可以得到最接近的是AJAX值发送到您的服务器,并返回验证结果。 – Passerby 2013-03-01 05:31:01

+1

相关:[如何连接Javascript到Python共享数据与JSON格式的两种方式?](http://stackoverflow.com/questions/11747527/how-to-connect-javascript-to-python-sharing-data-with -json-format-in-both-ways) – jfs 2013-03-01 06:23:04

您可以使用Ajax,这是更容易jQuery

$.ajax({ 
    url: "/path/to/your/script", 
    success: function(response) { 
    // here you do whatever you want with the response variable 
    } 
}); 

,你应该阅读the jQuery.ajax page,因为它有太多的选择。

+1

在我的情况下,这段代码从文件的URL – 2017-06-30 09:19:16

做一个页面(或服务)的蟒蛇,它可以接受邮递或get请求并处理信息,并返回一个响应。如果响应采用json格式,则会更好。然后,您可以使用此代码在点击按钮上拨打电话。

<input type="text" name="name" id="name"> 
<button type="button" id="home" onclick="validate()" value="checkvalue"> 
<script> 
$('#id').click(function(){ 

$.ajax({ 
     type:'get', 
     url:<YOUR SERVERSIDE PAGE URL>, 
     cache:false, 
     data:<if any arguments>, 
     async:asynchronous, 
     dataType:json, //if you want json 
     success: function(data) { 
     <put your custom validation here using the response from data structure > 
     }, 
     error: function(request, status, error) { 
     <put your custom code here to handle the call failure> 
     } 
    }); 
}); 
</script> 

我希望这有助于

+1

下载python脚本文件非常感谢你..它真的为我工作 – user2058205 2013-03-01 06:38:54

+0

乐于帮助。如果您不介意,请在答案被接受的情况下标记答案。 – Hari 2016-06-22 18:59:36

+0

@Hari请问$('#id')在做什么?这里用'#id'作为占位符来代替'home'之类的东西(在OP的问题中)?如果是这样,我仍然不确定$是在做什么 – 2016-11-23 13:48:17