【问题标题】:Setting a Selected Option in a Template Using jQuery or Coffeescript使用 jQuery 或 Coffeescript 在模板中设置选定选项
【发布时间】:2015-02-13 08:45:58
【问题描述】:

我有一个咖啡脚本,它创建下拉列表并根据返回值选择列表这是我的咖啡脚本

var op=operator;
html = """
 <select _name="op">
      <option value="" #{'selected' if op == ''}>=</options>
      <option value="$ne" #{'selected' if op == '$ne'}>&ne;</options>
      <option value="$lt" #{'selected' if op == '$lt'}>&lt;</options>
      <option value="$lte" #{'selected' if op == '$lte'}>&le;</options>
      <option value="$gt" #{'selected' if op == '$gt'}>&gt;</options>
      <option value="$gte" #{'selected' if op == '$gte'}>&ge;</options>
    </select>
 """
  el = $(html)

问题是如何在 jquery 中使用这个#{'selected' if op == '$gte'} 在 jquery 中是否有任何插件或库可以做到这一点? 我在选项之外尝试过,但我希望它在每个选项中都这样做。

【问题讨论】:

  • 不要在咖啡脚本中使用“var”。运营商从哪里来?

标签: jquery select coffeescript


【解决方案1】:

考虑在 html 模板之外使用 javascript 手动设置选择选项。

document.getElementById(op).setAttribute("selected", "true")

或者使用 jQuery

$("#" + op).attr("selected", "true")

请注意,这需要您将运算符作为 id 添加到每个选项元素。

【讨论】:

    猜你喜欢
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    相关资源
    最近更新 更多