gpt4 book ai didi

javascript - 对象没有被正确解释 JavaScript

转载 作者:行者123 更新时间:2023-11-28 02:27:59 24 4
gpt4 key购买 nike

即使控制台日志表明对象内的值已更改,div 的高度也没有正确更改。这是什么结果?我期望它会根据对象内部 0 的数量而改变。例如,如果有一个0,那么它会减少到25px,如果有零个0,它就会减少到0px。但是,即使所有输入都已填充,它也总是会减小到 50px。为什么?

https://jsfiddle.net/r4gf716f/

$(document).ready(function() {

var obj = {
userLength: 0,
passLength: 0,
};

$('#username').on('change', function() {
if ($('#username').val().length < 6) {
$('#para').html('too short - user');
obj.userLength = 0;
console.log('failed user');
} else {
$('#para').html('');
obj.userLength = 1;
console.log('WORKINGUSER');
}
})

$('#password').on('change', function() {
if ($('#password').val().length < 6) {
$('#para').html('too short - pass');
obj.passLength = 0;
console.log('failed pass');
} else {
$('#para').html('');
obj.passLength = 1;
console.log('passed user');
}
})


var errorCount = Object.keys(obj).filter(function(key) {
return (obj[key] === 0);
}).length;

if (errorCount > 0) {

$('.color').css("height", +errorCount * 25 + "px");
console.log(errorCount);
}

})

HTML

<body>
<p id='para'>

</p>

<input type='text' id='username' placeholder='u'>
<input type='text' id='password' placeholder='p'>

<div class='color'>

</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

</body>

CSS

.color {
background-color: black;
height: 200px;
width: 100px;
transition: all ease-in-out 2s;
}

最佳答案

就像 apsillers 说它只运行一次。我为您创建了一个函数来进行检查。您只需在开始时以及要检查错误时调用它。

    $(document).ready(function() {

var obj = {
userLength: 0,
passLength: 0,
};
checkErrors();
$('#username').on('change', function() {
if ($('#username').val().length < 6) {
$('#para').html('too short - user');
obj.userLength = 0;
console.log('failed user');
} else {
$('#para').html('');
obj.userLength = 1;
console.log('WORKINGUSER');
}
checkErrors();
})

$('#password').on('change', function() {
if ($('#password').val().length < 6) {
$('#para').html('too short - pass');
obj.passLength = 0;
console.log('failed pass');
} else {
$('#para').html('');
obj.passLength = 1;
console.log('passed user');
}
checkErrors();
})

function checkErrors(){
var errorCount = Object.keys(obj).filter(function(key) {
return (obj[key] === 0);
}).length;

if (errorCount > 0) {

$('.color').css("height", errorCount * 25 + "px");
console.log(errorCount);
}
}
})

编辑:

如果您希望它的 .color div 为 0px,这将是代码:

    $(document).ready(function() {

var obj = {
userLength: 0,
passLength: 0,
};
checkErrors();
$('#username').on('change', function() {
if ($('#username').val().length < 6) {
$('#para').html('too short - user');
obj.userLength = 0;
console.log('failed user');
} else {
$('#para').html('');
obj.userLength = 1;
console.log('WORKINGUSER');
}
checkErrors();
})

$('#password').on('change', function() {
if ($('#password').val().length < 6) {
$('#para').html('too short - pass');
obj.passLength = 0;
console.log('failed pass');
} else {
$('#para').html('');
obj.passLength = 1;
console.log('passed user');
}
checkErrors();
})

function checkErrors(){
var errorCount = Object.keys(obj).filter(function(key) {
return (obj[key] === 0);
}).length;


$('.color').css("height", errorCount * 25 + "px");
console.log(errorCount);
}
})

关于javascript - 对象没有被正确解释 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47841772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com