为什么我的 if 语句仅在我的输入具有值时才有效
Why is my if statement only work when my input has a value?
我正在使用jQuery构建一个五星评级系统。 该脚本创建五个星形图标。 当您将鼠标悬停在其中一个星星上时,之前的所有星星(包括悬停在上面的星星)都会突出显示。 单击星号时,脚本创建的隐藏输入的值将更改为该星号和该星号以及星号的编号,然后保持突出显示状态,直到单击其他星号为止。 当用户的鼠标离开星形的父星星时,如果之前选择了星形(基于隐藏输入的值),则这些星星将保持突出显示。 如果用户的鼠标离开父级时之前没有选择星号,则所有星号都将恢复为空...或者他们应该。
目前,当鼠标离开父级时,星星会返回选择,这就是我想要的。 问题是,如果未选择值(未单击星号),则星号不会返回为空。 父级的 mouseleave 方法中的 if 语句有什么问题? 这是一个jsFiddle。 我已将输入保留为文本输入,以便您可以看到值的变化。
.HTML:
<div class="rating" style="float: none;clear: both;">
<noscript>
<label class="radio-inline">
<input type="radio" name="stars" value="1" title="1 Star"> 1
</label>
<label class="radio-inline">
<input type="radio" name="stars" value="2" title="2 Stars"> 2
</label>
<label class="radio-inline">
<input type="radio" name="stars" value="3" title="3 Stars"> 3
</label>
<label class="radio-inline">
<input type="radio" name="stars" value="4" title="4 Stars"> 4
</label>
<label class="radio-inline">
<input type="radio" name="stars" value="5" title="5 Stars"> 5
</label>
</noscript>
</div>
j查询:
$element = $('.rating');
$element.each(function() {
$element.empty();
var hiddenInput = $('<input type="text" name="stars" id="selectedStars">');
$element.append(hiddenInput);
$selectedStars = $('#selectedStars').val();
for (var i = 0; i < 5; i++) {
var occurrence = i + 1;
var newStar = $('<i class="fa fa-star-o" title="' + occurrence + ' stars" data-occurrence="' + occurrence + '"></i>');
newStar.on('click', function() {
$('#selectedStars').attr('value', $(this).data('occurrence'));
$(this).prevAll().andSelf().removeClass('fa-star-o').addClass('fa-star');
$(this).nextAll().removeClass('fa-star').addClass('fa-star-o');
$element.data('selected', $(this).data('occurrence'));
console.log($selectedStars);
}).on('mouseover', function() {
$(this).prevAll().andSelf().removeClass('fa-star-o').addClass('fa-star');
$(this).nextAll().removeClass('fa-star').addClass('fa-star-o');
});;
$element.append(newStar);
}
$(this).mouseleave(function() {
$selectedStars = $('#selectedStars').attr('value');
if ($selectedStars != '') {
$('i[data-occurrence="' + $selectedStars + '"]').prevAll().andSelf().removeClass('fa-star-o').addClass('fa-star');
$('i[data-occurrence="' + $selectedStars + '"]').nextAll().removeClass('fa-star').addClass('fa-star-o');
console.log($selectedStars);
} else {
//This should return all stars to empty if no value was set.
$('i[class*="fa-star"]').removeClass('fa-star').addClass('fa-star-o');
}
});
});
如果未选择任何内容,则$selectedStars
undefined
,而不是''
。 如果您更改
if ($selectedStars != '') {
自
if ($selectedStars)
它按预期工作。
相关文章:
- 为什么不是'我的函数在解析云代码中工作吗?当我在Angular和Express中测试时,它是有效的
- 当我的所有 Ng-from 都有效时启用一个按钮
- 转换链接在jsfiddle中有效,但在我的浏览器上无效
- 为什么我的不正确的 if 语句有效
- 为什么这在 jsfiddle 中有效,但在我的文档中不起作用
- Jquery/Javascript检查我的单词是否有效
- 为什么这些代码在我的测试服务器上有效,而在jsfiddle上无效
- 为什么不't我的Javascript,如果其他方法有效的话
- 我的基于节点的咖啡脚本未运行(错误%1不是有效的应用程序.)
- 我的标记在 jsfiddle 中有效,但不适用于任何浏览器
- Openlayers 3 - 无法实现我的标记,但填充确实有效
- 为什么我的硒停止识别有效的javascript
- 为什么我的 if 语句仅在我的输入具有值时才有效
- 我的开关结构似乎有效(JavaScript 控制结构)
- 我的幻灯片网站仅在单击按钮两次以运行 JS 时才有效
- 为什么我的 javascript 函数只有在我手动输入输入时才有效
- 连接到测试 websocket 服务器有效,但连接到我的 Node.js 服务器失败
- 为什么我的第一个截图有效,而我的第二个截图不起作用
- 为什么我得到“;错误100:只能在应用程序的有效测试用户上调用此方法;
- 需要帮助使我的HTML和CSS代码的导航效果更有效