如何知道我在烧瓶中点击了哪个按钮?

时间:2013-09-28 14:11:07

标签: python flask wtforms flask-wtforms

我当前的项目,我的索引页面显示了几个种子,每个种子都有一个按钮,用于启动或停止种子。

我使用表单和循环创建页面,因此表单始终具有相同的名称。但是我需要知道用户点击了哪个按钮才能知道要停止哪个torrent!

以下是模板:

{% block content %}
 <h1>Hello, {{user}} !</h1>

 {% for torrent in torrents %}
      <form action="/index" method="post" name="torrent">
           {{torrent.control.hidden_tag()}}
           <p><a href='/torrent/{{torrent.id}}'>{{torrent.name}}</a> is {{torrent.status}} and {{torrent.progress}} % finished. <input type="submit" value="Start / Stop">
           </p>
      </form>
  {% endfor %}

以下是观点:

  @app.route('/index', methods = ['GET', 'POST'])
  def index():
  user = 'stephane'

  torrents = client.get_torrents()

  # for each torrent, we include a form which will allow start or stop
  for torrent in torrents:
    torrent.control = TorrentStartStop()
    if torrent.control.validate_on_submit():
        start_stop(torrent.id)

return render_template("index.html", title = "Home", user = user, torrents = torrents)

那么,我如何知道用户想要停止/启动哪个torrent?

3 个答案:

答案 0 :(得分:2)

假设hidden_tag创建了一个名为hidden的{​​{1}}输入,并且输入了torrent的ID,您会在torrent_id中找到该torrent ID:

request.form["torrent_id"]

答案 1 :(得分:0)

使用HTML <button type="submit">元素并将value设置为信息哈希,或者设置包含信息哈希的formaction网址(formaction仅限HTML5)

答案 2 :(得分:0)

如果有人单击按钮并将其css类更改为活动按钮,然后检查哪个按钮处于活动状态并获取其信息,您可以在js中使用活动按钮状态。

这是js代码:

var btnContainer = document.getElementById("myDIV");

var btns = btnContainer.getElementsByClassName("btn");

for (var i = 0; i < btns.length; i++) {
 btns[i].addEventListener("click", function() {
  var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
 });
  }

这是按钮的css:

  .btn {
    border: none;
    outline: none;
    padding: 10px 16px;
    background-color: #f1f1f1;
    cursor: pointer;
    }


   .active, .btn:hover {
    background-color: #666;
    color: white;
    }
相关问题