我有一个从服务器响应返回的数组,它有3种可能性,我需要检查UI;
- 如果长度是0..do某事
- 如果长度为1 ...做某事
- 长度> 1 ...做点什么
在上述条件下,有没有更好的方法来编写下面的JS代码?
if (myArray.length == 0) {
} else if (myArray.length == 1) {
} else {
}
Run Code Online (Sandbox Code Playgroud) 我有多个span
元素,并希望在显示以下文字span
位置(10
,19
,28
,37
)是红色的.
我试过以下;
span {
&:nth-of-type(9n+1) {
color: red;
}
}
Run Code Online (Sandbox Code Playgroud)
唯一的事情是第一个跨度也得到了我不想要的CSS应用.它应该从位置开始10
.我怎样才能解决这个问题?
在我的 Handlebars 模板中,我想根据“selectedRows”数组长度启用/禁用按钮(即,如果 selectedRows > 0 则启用,否则禁用它,即数组长度为 0)我想避免在组件 JS 中引入任何额外的实例变量. 是否可以直接在车把中执行此操作?
即像下面这样的东西
{{my-button count=selectedRows enabled=(if selectedRows > 0)}}
Run Code Online (Sandbox Code Playgroud)
启用是布尔值 (true/.false)
我有一个包含多行输入文本框的网格。我正在尝试动态计算/显示每行文本框的当前位置和剩余字符数(每个文本框最多可以是 100 个字符)
$("#mygrid tbody").on('focusin', function(e) {
if (e.target.tagName.toLowerCase() === 'input') {
let currentPosition = e.target.value.length === 0 ? 1 : e.target.value.length + 1;
let remainingCharacters = 100 - e.target.value.length;
}
});
$("#mygrid tbody").on('keydown', function(e) {
if (e.target.tagName.toLowerCase() === 'input') {
console.log("e.target.selectionStart : " + e.target.selectionStart);
console.log("e.target.value : " + e.target.value);
let currentPosition = e.target.selectionStart + 1;
let remainingCharacters = 100 - e.target.value.length;
}
});
Run Code Online (Sandbox Code Playgroud)
我看到的是,控制台日志语句显示有 1 个键的延迟,即当用户逐个键输入“123”时,记录的内容如下;
e.target.selectionStart : 2
e.target.value : 12
Run Code Online (Sandbox Code Playgroud)
我很困惑为什么会发生这种情况?
我的 BackboneJS 模板中有以下代码(使用 Underscore 渲染)
<input type="radio" class="isMale" id="isMaleVolunteersNo"
<%= model.attributes.isMaleVolunteers == undefined ||
model.attributes.isMaleVolunteers == null ||
model.attributes.isMaleVolunteers == 'N' ? 'checked' : ''
%>
name="isMaleVolunteers" value="false" />
<label for="isMaleVolunteersNo" style="display:inline">No</label>
Run Code Online (Sandbox Code Playgroud)
当模型属性 isMaleVolunteers 未定义时,我希望默认选中“否”。
我尝试了以下(带括号)...仍然不起作用;
<input type="radio" class="isMale" id="isMaleVolunteersNo"
<%= (model.attributes.isMaleVolunteers == undefined ||
model.attributes.isMaleVolunteers == null ||
model.attributes.isMaleVolunteers == 'N') ? 'checked' : ''
%>
name="isMaleVolunteers" value="false" />
<label for="isMaleVolunteersNo" style="display:inline">No</label>
Run Code Online (Sandbox Code Playgroud)
但上面的代码不起作用。代码有问题吗?
javascript backbone.js underscore.js underscore.js-templating
我想根据以下内容编写三元运算符;
var statusJSON = {
'- Select -': '',
'Active': true,
'Inactive': false
};
Run Code Online (Sandbox Code Playgroud)
目前我有
statusFlag: $('#statusFlag').val() == 'true' ? true : false
Run Code Online (Sandbox Code Playgroud)
这适用于true
和false
值,但不处理第三个条件(即空""
)
我该如何处理?