简体   繁体   English

使用jQuery或Coffeescript在模板中设置所选选项

[英]Setting a Selected Option in a Template Using jQuery or Coffeescript

I have a coffeescript that creates dropdown list and select the list depend on returned value here is my coffeescript 我有一个创建下拉列表并根据返回值选择列表的coffeescript,这是我的coffeescript

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)

the question is how to use this #{'selected' if op == '$gte'} inside jquery is there any plugin or library to do that in jquery ? 问题是如何在jquery中使用此#{'selected' if op == '$gte'} ,jquery中是否有任何插件或库可以做到这一点? i tried outside the option but i want it inside each option to do that. 我尝试在选项之外,但我想在每个选项内做到这一点。

Consider manually setting the select option with javascript outside of your html template. 考虑在HTML模板之外使用javascript手动设置选择选项。

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

Or using jQuery 或者使用jQuery

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

Note that this would require you to add the operator as the id to each option element. 请注意,这将要求您将运算符作为id添加到每个选项元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM