首页 > 解决方案 > 在 Flask 和 Javascript 中禁用和启用按钮

问题描述

可以在 Flask 中禁用和启用按钮吗?我有POST请求Ajax并将数据从 JavaScript 发送到 Flask,并从这个数据中创建response一个带有弹出窗口的标记,并且有两个按钮onetwo. 当用户单击时,one他被添加到数据库中。在 Flask 中,我检查用户是否在数据库中,当他在数据库中时,此按钮对他禁用。这两个按钮也是提交表单的,我不知道我是否可以这样做。

代码烧瓶:

@app.route('/mapaa',methods=['GET','POST'])
def mapa():
if request.method == "POST":
  if 'join-form' in request.form:
          //check if is in the database

  elif 'remove-form' in request.form:
          //check if he is signed to the event

          else:
            name_ev = request.form.get('nameevent')
            all_data = Event.query.filter_by(name=name_ev).all()
            for row in all_data:
                date_st_string = str(row.date_start)
                date_end_string = str(row.date_end)
                dictionary = {'id':row.id,'date_st':date_st_string,'date_end':date_end_string,'type':row.type,'name':row.name,'len_route':row.len_route,'route':row.route}
            return jsonify(slownik)

           // data to create marker

JavaScript 代码:

$("#search-button_event").one('click', function(e) { // make ajax request on btn click
  e.preventDefault();
  $.ajax({
    type: "POST",
    url: "/mapaa", // url to the function
    data: {
      nameevent: $("#name_of_event").val(), // value of the form
    },
    success: function(response) {
      id = (response['id']);
      data_end = (response['date_end']);
      data_st = (response['date_st']);
      len_route = (response['len_route']);
      name = (response['name']);


      let marker_event = L.marker(array[0]).bindPopup()
      marker_event._popup.setContent(
        '<form method="POST" action="/mapaa"' +
        '<p>Name: ' + nazwa + '</p>' +
        '<input name="nameOfEvent" type="hidden" value="' + nazwa + '" id="nameOF">' +
        '<input id="ev_n" name="idOfEvent" type="hidden" value="' + id + '" id="idOF">' +
        '<p>Date start: ' + data_st + '</p>' +
        '<p>Date end: ' + data_end + '</p>' +
        '<p>Typ: ' + typ + '</p>' +
        '<p>Len route: ' + len_drogi + '</p>' +
        '<button type="submit" id="one" name="join-form"  class="btn btn-warning btn-block">Join to event</button>' +
        '<br>' +
        '<br>' +
        '<button type="submit" id="two" name="remove-form"  class="btn btn-danger btn-block">Sign out from the event</button>' +
        '</form>')

      marker_event.addTo(mymap)

      polyline_event = L.polyline(array, {
        color: 'red'
      })
      marker_event.on('click', function() {
        polyline_event.addTo(mymap)
      })
      marker_event.getPopup().on('remove', function() {
        polyline_event.remove()
      })
      mymap.setView(array[0], 14)


      document.getElementById("name_of_event").value = "";



    },
  });
});

如果用户在数据库中,我想这样做,他将有禁用按钮加入。

标签: javascriptpythonflask

解决方案


推荐阅读