在button cli上执行python脚本

2024-04-27 15:14:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个只有一个按钮的HTML页面,当我们单击按钮并返回到带有结果的相同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代码正在对文本框中输入的名称进行一些验证,并给出返回状态。

但我需要将结果放回同一页,这样我可以在稍后提交表单时提供所有详细信息。任何帮助都将不胜感激


Tags: 代码textnametest脚本idinputhtml
2条回答

可以使用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,因为它有太多的选项。

在python中创建一个页面(或服务),它可以接受post或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>

我希望这能有帮助

相关问题 更多 >