【问题标题】:if stament for atrribute with no value无值属性的 if 语句
【发布时间】:2016-05-29 14:31:37
【问题描述】:

我有两个 <input> 元素。它们非常相似。 所以我尝试在一个地方定义它们。

对于具有不同值的属性,我可以写如下内容:

input(type="text", placeholder="{{(address.$index == 0)? ph1 : ph2}}",  .....)

但是当属性没有值时,我不知道如何编写 if/else 逻辑。 例如,我需要基于address.$index 值的<input> 元素中的autofocus 属性

更新: 我的 *.jade 文件

#my-input
   .my-input
       input(type="text", 
             autofocus="{{(condition == 0)? true : false}}", 
             placeholder="{{(condition == 0)? 'focused onLoad' : 'not focused'}}")

我的实际结果是:

<div class="my-input">
   <input type="text" autofocus="true" placeholder="focused onLoad">
</div>
......
<div class="my-input">
   <input type="text" autofocus="false" placeholder="not focused">
</div>

但我需要:

<div class="my-input">
   <input type="text" autofocus placeholder="focused onLoad">
</div>
......
<div class="my-input">
   <input type="text" placeholder="not focused">
</div>

【问题讨论】:

  • 尝试使用 $index 从循环外部访问输入不是一个好方法。请提供更多代码。包括你的 v-for 在哪里,以及用于它的数据属性。
  • 其实我没有在我的源代码文件中看到v-for。但我会尝试添加更多代码来解释我的问题。
  • 无视我的评论。这里有一个 vue.js 的标签,我以为你在使用它。
  • 我使用它,但在代码简化后(出于“简单示例”的目的)vue.js 代码消失了。我认为我的解决方案是在v-focusv-focus-auto 或类似的领域。但我不明白如何正确烹饪。

标签: node.js pug vue.js


【解决方案1】:

使用此代码,它可以工作:

    div.my-input 
            input(type="text", autofocus="",placeholder="focused onLoad")

div.my-input
            input(type="text",placeholder="not focused")


<div class="my-input"> 
  <input type="text" autofocus="" placeholder="focused onLoad"/>
</div>
<div class="my-input">
  <input type="text" placeholder="not focused"/>
</div>

就像你可以在这里看到的演示 link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-01
    • 2010-12-09
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    相关资源
    最近更新 更多