javascript - 如何检索返回的 JS 函数值并将其传递给 Flask

标签 javascript python html flask

我有以下下拉菜单,它们的值是从 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)

最佳答案

在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>

关于javascript - 如何检索返回的 JS 函数值并将其传递给 Flask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58236429/

相关文章:

python - AES Python 加密和 Ruby 加密 - 不同的行为?

javascript - 为什么 jQuery Map 会自动展平返回的数组?

javascript - 美化大一行 JSON 文件

javascript - 是否有可以在 Google App Script 中使用的 .join() 替代方案?

python - Pandas :处理值后提取索引和列

html - Bootstrap 日期时间选择器中的 CSS 问题

javascript - 确定主要现代浏览器的浏览器类型和版本

python datetime timestrp函数错误

单击时 JavaScript/HTML 图像发生变化

html - 我希望导航栏中的下拉菜单将内容推送到其下方