【问题标题】:Return Id of clicked a tag点击标签的返回ID
【发布时间】:2021-08-25 08:45:51
【问题描述】:

我使用flask在html页面中显示SQL表,例如:

.html 页面:

    <tbody>
    {% for row in myresultt %}
    <tr>
        <td>{{row[1]}}</td>
        <td>{{row[2]}}</td>
        <td>{{row[3]}}</td>
        <td>{{row[4]}}</td>
        <td class="pointer" data-toggle="modal" data-target="#exampleModal">
            <i class="fas fa-eye text-gray px-1"></i>
            10
        </td>
        <td style="width: 25%;">
            <ul class="d-flex justify-content-between align-items-center">
                <li class="mx-1">
                    <button class="btn gold-color-bg white-color font-12">
                        <a class="text-white" id="button" name="button" value="{{ row[0] }}" href="{{ url_for('join_s_mod')}}">start</a>
                    </button>
                </li>
            </ul>
        </td>
    </tr>
{% endfor %}
</tbody>

HTML 页面截图

数据库表截图

.py 代码:

@flask.route('/next_lecture', methods=['POST'])
def join_s_mod():
    if request.method == 'POST':
        button= request.form['button']
    cursor = mysql.connection.cursor()
    cursor.execute('SELECT * FROM `sessioncreate` WHERE `ownerId` = %s', str(button))
    myresult = cursor.fetchall()
return render_template("next-lecturee.html", myresultt=myresult)
  

当我点击 HTML 页面的开始按钮时我需要返回行的 id 并将这个 id 值作为变量传递给 SQL 查询。

【问题讨论】:

  • 如果你可以使用GET方法而不是通过url_forlinkother answer
  • @gaurav 我尝试像 {{ url_for('join_s_mod'), id=row[0]}} 一样传递它,但返回错误
  • 错误来自烧瓶函数或 pymysql ?你应该在执行函数中使用元组,比如cursor.execute('SELECT * FROM sessioncreate WHERE ownerId = %s', (id,))link

标签: python sql python-3.x flask


【解决方案1】:

尝试使用它,我自己从来没有遇到过问题,从 HTML 15.3 开始就一直在使用它

<button id="1" onClick="reply_click(this.id)">B1</button>
<button id="2" onClick="reply_click(this.id)">B2</button>
<button id="3" onClick="reply_click(this.id)">B3</button>
    
<script type="text/javascript">
  function reply_click(clicked_id)
  {
      alert(clicked_id);
  }
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-22
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    相关资源
    最近更新 更多