在if/else语句中,e.preventDefault()并未对条件执行100% [英] in an if/else statement, e.preventDefault() is not performing 100% to the conditions
本文介绍了在if/else语句中,e.preventDefault()并未对条件执行100%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此功能(使用option:selected
)更改<div class="item">0</div>
的CSS和HTML,以模拟一种进度条应用程序.问题是它超过了100%
.那么if
语句不正确还是这里有问题?有任何线索吗?
This function (using option:selected
) changes the CSS and HTML of <div class="item">0</div>
to simulate a kind of progress bar app. The problem is that it exceeds 100%
. So the if
statements are not correct or something here is wrong? Any clues?
$(function(){
$('#selectChoice').change(function() {
$('option:selected', this).each(function() {
var id = $('#selectChoice option:selected').attr('id');
$('#' + id + 'Item').css("color", "#f00").css("font-weight", "bold");
});
});
$('#plus25').on('click', function() {
var id = $('#selectChoice option:selected').attr('id');
$('#' + id + 'Item').html(function(i, value) {
var newWidth = parseInt(value * 1 + 25);
var e = window.event;
$('#' + id + 'Item').css('width', newWidth + '%');
if (value <= 75){
return newWidth;
} else {
e.PreventDefault();}
});
});
});
以下情况也在0
if (value >= 25) {
return newWidth;
}
可以在此处
推荐答案
您应该使用传递给click
处理程序的event
对象,而不是全局window.event
.同样,该方法为小写的preventDefault()
.试试这个:
You should use the event
object passed in to the click
handler, not the global window.event
. Also, the method is preventDefault()
in lowercase. Try this:
$('#plus25').on('click', function(e) { // Note the 'e' parameter here
var id = $('#selectChoice option:selected').attr('id');
$('#' + id + 'Item').html(function(i, value) {
var newWidth = parseInt(value * 1 + 25);
$('#' + id + 'Item').css('width', newWidth + '%');
if (value <= 75) {
return newWidth;
} else {
e.preventDefault();
}
});
});
这篇关于在if/else语句中,e.preventDefault()并未对条件执行100%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文