Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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 e、 preventDefault()在if/else条件下无法100%工作_Jquery_Css - Fatal编程技术网

Jquery e、 preventDefault()在if/else条件下无法100%工作

Jquery e、 preventDefault()在if/else条件下无法100%工作,jquery,css,Jquery,Css,此函数(使用选项:selected)更改0的CSS和HTML以模拟一种进度条应用程序。 问题是它超过了100%。那么当返回“newWidth”时if语句是不正确的,还是有其他错误?如有任何线索,我们将不胜感激 $(function(){ $('#selectChoice').change(function() { $('option:selected', this).each(function() { var id = $('#selectChoice option:se

此函数(使用选项:selected)更改0的CSS和HTML以模拟一种进度条应用程序。 问题是它超过了100%。那么当返回“newWidth”时if语句是不正确的,还是有其他错误?如有任何线索,我们将不胜感激

$(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();}
        });
    });
});
$(函数(){
$('#selectChoice')。更改(函数(){
$('option:selected',this).each(函数(){
var id=$('#selectChoice选项:selected').attr('id');
$(“#”+id+“Item”).css(“color”,“#f00”).css(“font-weight”,“bold”);
});
});
$('#plus25')。在('单击',函数()上){
var id=$('#selectChoice选项:selected').attr('id');
$('#'+id+'Item').html(函数(i,值){
var newWidth=parseInt(值*1+25);
var e=window.event;
$('#'+id+'Item').css('width',newWidth+'%');

if(value如果您试图阻止click事件冒泡,请将click事件传递给
html()
函数;情况如此

$('#plus25').on('click', function(e) {
    var id = $('#selectChoice option:selected').attr('id');
    var idItem = $('#' + id + 'Item');
    idItem.html(function(i, value) {
        var newWidth = +value + 25;
        idItem.css('width', newWidth + '%');
        if (newWidth < 76){
            return newWidth;
        } else { 
            e.preventDefault();
            return "";
        }
    });
});
$('plus25')。在('click',函数(e)上{
var id=$('#selectChoice选项:selected').attr('id');
变量idItem=$(“#”+id+“项”);
html(函数(i,值){
var newWidth=+值+25;
css('width',newWidth+'%');
如果(新宽度<76){
返回新宽度;
}否则{
e、 预防默认值();
返回“”;
}
});
});
$('plus25')。在('click',函数(e)上{
变量id=$('#selectChoice选项:selected').attr('id'),
$target=$('#'+id+'Item');
$target.html(函数(i,值){
var currentWidth=parseInt(值,10),
新宽度=当前宽度+25;
//首先检查操作是否有效

if(newWidth)我不知道你在哪里检查值+change>100。从逻辑上看,你似乎想检查值+change<100,如果真的使用值,否则设置为100。你认为
e.PreventDefault()
有什么作用?(我不是想以屈尊的方式问这个问题,我只是好奇。)它阻止事件在其他事件处理程序中进一步求值。它不会停止您当前的求值。这就是为什么您应该在打开开发人员工具的情况下测试代码的原因…太棒了!!!非常感谢您,请查看修订版:如果您将来可以帮助我,请将详细信息发送给我s和费率(可能是类似问题)
$('#plus25').on('click', function(e) {
    var id = $('#selectChoice option:selected').attr('id'),
        $target = $('#'+ id +'Item');

    $target.html(function(i, value) {
        var currentWidth = parseInt(value, 10),
            newWidth = currentWidth + 25;

        //check if the operation is valid first
        if (newWidth <= 100) {
            $target.css('width', newWidth +'%');
        } else {
            //update would have pushed it beyond 100.
            //keep the original value
            //don't change the css
            newWidth = currentWidth;
        }

        return newWidth;
    });
});