Javascript 玉石选择字段填充数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10362959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:02:50  来源:igfitidea点击:

Jade select field populating data

javascriptnode.jspug

提问by Risto Novik

Is there any better ways to populate Jade based select fields, I am currently using this example. Is there any better ways to not ruin the template code?

有没有更好的方法来填充基于 Jade 的选择字段,我目前正在使用这个例子。有没有更好的方法来不破坏模板代码?

the item value is 'day' example.

项目值是“天”示例。

    select
      repeation = [ 'no-repeat', 'day', 'week', 'month']
      for item in repeation
        if job.repeat == item
          option(selected="true") #{item}
        else
          option #{item}

Also what about displaying multiple selections, when the item is array of ['day', 'week']?

另外,当项目是 ['day', 'week'] 数组时,如何显示多个选择?

// Edit small possible solution for multiple element

// 编辑多个元素的小可能解决方案

      enginges = [ 'google', 'bing', 'yahoo', 'duckduckgo']
      for engine in enginges
        option(selected=job.sources.indexOf(engine) != -1) #{engine}

回答by AntelopeSalad

You should be able to do something like:

您应该能够执行以下操作:

for item in repeation
  option(selected=job.repeat == item) #{item}

The same concept should be able to be applied to a multiple item select drop down.

相同的概念应该能够应用于多个项目选择下拉列表。

回答by Rico Rodriquez Collins

A couple of things to add to the answer (https://stackoverflow.com/a/10368381/870274):

有几件事要添加到答案中(https://stackoverflow.com/a/10368381/870274):

  1. "each" is more commonly used now instead of "for"

  2. don't forget the "-" for the line: repeation = [ 'no-repeat', 'day', 'week', 'month'] ,or you will get a compilation error. So the final result would be (same as yours):

    select
      - repeation = [ 'no-repeat', 'day', 'week', 'month']
      each item in repeation
        option(selected=job.repeat == item) #{item}
    
  1. “each”现在更常用而不是“for”

  2. 不要忘记行的“-”:repeat = [ 'no-repeat', 'day', 'week', 'month'] ,否则你会得到一个编译错误。所以最终结果将是(与您的相同):

    select
      - repeation = [ 'no-repeat', 'day', 'week', 'month']
      each item in repeation
        option(selected=job.repeat == item) #{item}