Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 在if/else语句中,e.preventDefault()未对条件执行100%_Jquery - Fatal编程技术网

Jquery 在if/else语句中,e.preventDefault()未对条件执行100%

Jquery 在if/else语句中,e.preventDefault()未对条件执行100%,jquery,Jquery,此函数(使用选项:选中的)更改0的CSS和HTML,以模拟一种进度条应用程序。问题是它超过了100%。那么如果语句不正确或者这里有什么错误?有什么线索吗 $(function(){ $('#selectChoice').change(function() { $('option:selected', this).each(function() { var id = $('#selectChoice option:selected').attr('i

此函数(使用
选项:选中的
)更改
0
的CSS和HTML,以模拟一种进度条应用程序。问题是它超过了
100%
。那么
如果
语句不正确或者这里有什么错误?有什么线索吗

$(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();}
            });
        });
    });

可以看到完整的版本

您应该使用传递到
单击
处理程序的
事件
对象,而不是全局
窗口。事件
。此外,该方法是小写的
preventDefault()
。试试这个:

$('#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();
        }
    });
});
$('#plus25')。在('click',函数(e){//注意此处的'e'参数
var id=$('#selectChoice选项:selected').attr('id');
$('#'+id+'Item').html(函数(i,值){
var newWidth=parseInt(值*1+25);
$('#'+id+'Item').css('width',newWidth+'%');

if(value)下面的IE bug修复是由于@amitthk:else{safePreventDefault(e);}}}});safePreventDefault=function(evt){//IE bug修复evt=evt | | window.event;if(evt){evt.returnValue=false;}if((evt)&&(evt.preventDefault){evt.preventDefault();}};获取
e未定义
。使用
函数(e)
.remove
var e=window.event;
谢谢@Parth Trivedi.maybe
var e=evt | | | | window.event;
需要在其他地方定义吗?谢谢@Rory McCrossan,但是它仍然超过了100%使用
newWidth<100
而不是
值<75
。我认为
如果
/
/else
需要更多的条件。。我到底想问什么呢?也许
否则如果
你最好开始问一个新问题,因为这个问题是关于
preventDefault()
$('#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();
        }
    });
});