【问题标题】:How do I get python code to execute properly from a button click when an alert is included as action from the button click?当警报包含在按钮单击的操作中时,如何通过单击按钮正确执行 python 代码?
【发布时间】:2016-07-27 19:32:41
【问题描述】:

我在使用同样调用 url 的 html 按钮时无法获得警报。更详细地描述这个问题,我有一个按钮,当按下它时会调用一个 url,它从我的 urls.py 文件和 views.py 文件调用一些播放视频的 python 代码,但是当我包含一个警报以在按下按钮时触发,python代码没有被执行,但警报显示没有任何问题。我的问题是如何使这两个操作在单击一次按钮时兼容,以便两者都能正确执行?如果提供更多信息,也可以使用 Django 框架。

这是html和javascript

<script type="text/javascript">
function show_alert() {
alert("The View Is Now Playing in a New Window");
} 
</script>
<a class="btn btn-default" href="/VideoPlayer" role="button"  onclick="show_alert()">Play Video</a>

【问题讨论】:

    标签: javascript python html django


    【解决方案1】:

    如果你想执行服务器端和客户端代码,你需要使用 XHR 或简单地Ajax

    &lt;a href=''&gt;&lt;/a&gt; 会将页面重定向到href 的网址,为此您需要在 JavaScript 点击方法中添加“return false”

    喜欢这个

    `function click(){
       alert();
       // call your ajax here
       return false;
    }`
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-01
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      相关资源
      最近更新 更多