javascript - django - 无法将选定的标签传递到js(ajax代码)

标签 javascript ajax django onclick html-select

我的选择.html:

<input type="submit" onclick="loadDoc2()" value="continue" style="height:50px;">

<select style="direction: rtl;width:200px;height:50px;">
    {% for item in items %}
        <option class="shahr-options" value="{{ item.shahr }}"> {{ item.shahr }}</option>
    {% endfor %}
</select>

这是 script2.js:

function loadDoc2(shahr) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      document.getElementById("ajax").innerHTML = xhttp.responseText;
    }
  };
  xhttp.open("GET", "/boxes=" + shahr, true);
  xhttp.send();
}

如何将用户选择传递给 onclick="loadDoc2()"

最佳答案

你可以做类似的事情

function loadDoc2(value) {
  alert(value);
}
<input type="submit" onclick="loadDoc2(document.getElementById('shahr-select').value)" value="continue" style="height:50px;">

<select id="shahr-select" style="direction: rtl;width:200px;height:50px;">
  <option class="shahr-options" value="1">first</option>
  <option class="shahr-options" value="2">second</option>
  <option class="shahr-options" value="3">third</option>
</select>

选择上的.value将获取所选值

关于javascript - django - 无法将选定的标签传递到js(ajax代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36514609/

相关文章:

javascript - 将数据与 HTML 元素相关联(不使用 jQuery)

javascript - 将数据表的行关联到 Bootstrap 开关(使用另一个列字段)

javascript - 使用ajax的select2加载数据无法选择任何选项

python - 可以在 Django {% ifequal %} 表达式中使用带有查找的变量吗?

javascript - ES6使用requestanimationframe的递归方法

javascript - 表展开和折叠在 angularjs 中不起作用

javascript - 测试 react 组件 : Jest encountered an unexpected token

jquery - MVC 中的空值多选级联下拉列表

django - Django 模型中的 OneToOne 字段

sql - 在 Django 查询中提供一个 LIMIT 参数而不需要获取 QuerySet 的一部分