Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.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条件工作不正确_Jquery_If Statement_Drupal 7_Logic - Fatal编程技术网

JQuery条件工作不正确

JQuery条件工作不正确,jquery,if-statement,drupal-7,logic,Jquery,If Statement,Drupal 7,Logic,这很奇怪,但可能很简单 我正在做一个简单的if来检查最低的数字。 我有调试语句,它们向我显示JQuery正在返回10this.Id作为字符串返回。将其解析为int var lowID = 50; var currentId; $('.tip').each(function() { currentId = parseInt(this.id); console.log('Tip id = ' + currentId ); if(currentId < lowID)

这很奇怪,但可能很简单

我正在做一个简单的if来检查最低的数字。
我有调试语句,它们向我显示JQuery正在返回10this.Id作为字符串返回。将其解析为int

var lowID = 50;
var currentId;

$('.tip').each(function() {
    currentId = parseInt(this.id);

    console.log('Tip id = ' + currentId );
    if(currentId  < lowID){
        console.log(currentId + '<' + lowID);

        lowID = currentId;
    }
});
var lowID=50;
var-currentId;
$('.tip')。每个(函数(){
currentId=parseInt(this.id);
log('Tip id='+current id);
如果(当前ID<低ID){

console.log(currentId+”ID属性是一个字符串,因此,
将其解析为int没有用,因为在此之前它被使用了三次……您应该在scopeThank You@MarshallOfSound的开头将其解析为int。我修复了上述代码。currentId将在每次迭代中重复使用。在.each(…)之外创建它将防止在每次迭代时重新创建变量。在本例中,这不是一个要求,但总体上我发现关注变量的范围是有益的。我不是在质疑范围决策,只是为什么要将其设置为null,为什么不定义它并将其保留在适当的位置。我接受编辑。在实例化时将变量设置为null是其他语言遗留下来的习惯。谢谢@MarshallOfSound。
"Tip id = 2" lightbox.js:6
"2<50" lightbox.js:8
"Tip id = 3" lightbox.js:6
"Tip id = 4" lightbox.js:6
"Tip id = 5" lightbox.js:6
"Tip id = 6" lightbox.js:6
"Tip id = 7" lightbox.js:6
"Tip id = 8" lightbox.js:6
"Tip id = 9" lightbox.js:6
"Tip id = 10" lightbox.js:6
"10<2" lightbox.js:8
"Tip id = 11" lightbox.js:6
"Tip id = 12" lightbox.js:6
"Tip id = 13" lightbox.js:6
"Tip id = 14" lightbox.js:6
"Tip id = 15"
var lowID = 50;
var currentId;

$('.tip').each(function() {
    currentId = parseInt(this.id);

    console.log('Tip id = ' + currentId );
    if(currentId  < lowID){
        console.log(currentId + '<' + lowID);

        lowID = currentId;
    }
});
var lowID = 50;

$('.tip').each(function() {
    var currentID = parseInt(this.id);
    console.log('Tip id = ' + currentID);
    if(currentID < lowID){
        console.log(currentID + '<' + lowID);
        lowID = currentID;
    }
});