【问题标题】:How to retrieve returned JS functions values and pass it in Flask如何检索返回的 JS 函数值并将其传递给 Flask
【发布时间】:2020-02-02 19:03:16
【问题描述】:

我有以下下拉菜单,它们的值是从 MongoDB 重新调整的。

HTML

<form>
  <div class="answer1wrap">
    <select id="mySelect1">
        <option value="void">Choose your answer</option>
        {% for row in rows %}
            <option value="{{row}}">{{row}}</option>
        {% endfor %}
    </select>
  <select id="mySelect2">
       <option value="void">Choose your answer</option>
        {% for row in rows %}
            <option value="{{row}}">{{row}}</option>
        {% endfor %}
    </select>
  </div>
</form>
<button class="btn btn-default" id="checkbtn" onclick="answers_1();answers_2();" type="button"><span    class="glyphicon glyphicon-check"></span> Check answers</button>

JS

<script>
  function answers_1() {
     var select = document.getElementById("mySelect1");
     var answer = select.options[select.selectedIndex].value;
     return answer;
   }
   function answers_2() {
      var select = document.getElementById("mySelect2");
      var answer = select.options[select.selectedIndex].value;
      return answer;
   }
</script>

我的目标是使用两个 JS 函数的返回值 (ObjectID),然后使用它们来获取该集合中的特定数据。

我有以下函数,其中key_1key_2 是从JS 函数返回的值,但不知何故我无法使其工作。

@app.route("/distance_object", methods=['POST', 'GET'])
def distance_object():
    row_object = []
    cursor = object_collection.find({}, {"_id": 1})
    for document in cursor:
        row_object.append(document['_id'])
    input_1 = object_collection.find({"_id": ObjectId(key_1)}, {"x_input": 2})
    input_2 = object_collection.find({"_id": ObjectId(key_2)}, {"x_input": 2})
    return render_template("distance.html", rows=row_object)

【问题讨论】:

    标签: javascript python html flask


    【解决方案1】:

    在 JS 中像那个函数一样使用

    <script>
      function answers_1() {
         var select = document.getElementById("mySelect1");
         return select.value;
       }
       function answers_2() {
          var select = document.getElementById("mySelect2");
          return select.value;
       }
    </script>
    

    【讨论】:

    • 谢谢!但是,我编写了与您回答的过程相同的 JS 函数。
    猜你喜欢
    • 2016-02-13
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多