2017-10-11 48 views
-1

显示的文本的性质我有一个变量,如:使用瓶,以确定在JavaScript

myfruits = ['Apple', 'Banana', 'Lemon'] 
havefruit = [True, True, False] 

根据用户的输入,在havefruit值可能会有所不同。如何在我的HTML文件中使用JavaScript使myfruits中对应于True的条目显示为一种颜色,并将对应于False的条目显示为不同的颜色?

+0

什么,特别是,你有麻烦用?获取输入?发送到模板?用JavaScript读取它?着色输出?请看[问]并用[mcve]澄清你的问题。 – davidism

+0

我有python输入/输出。将输出发送到模板,然后如何让网页显示它。我知道如何在Python中使用if语句来做到这一点,什么不是,但不是如何在javasript中使用if语句 –

+0

如何使模板显示输出 –

回答

0

您可以使用zip对数据进行格式化,然后模板检查,如果该值为True:

@app.route("/", methods=["GET", "POST"]) 
def homepage(): 
    myfruits=['Apple', 'Banana', 'Lemon'] 
    havefruit=[True, True, False] 
    final_data = {a:b for a, b in zip(myfruits, havefruit)} 
    return flask.render_template("file.html", variables=final_data) 

然后,在file.html

<html> 
    <select id="fruits" name="the_fruits"> 
     <option value=""></option> 
     {%for a, b in variables.items()%} 
     {%if b%} 
      <option>{{college}}</option> 
     {%endif%} 

     {%endfor%} 
    </select> 
</html>