gpt4 book ai didi

JavaScript 比较 if 不起作用?

转载 作者:行者123 更新时间:2023-11-28 19:08:09 25 4
gpt4 key购买 nike

我有以下代码来检查汽车的最大值。但是每当我在表中选择 10 时,我都没有得到与汽车相对应的最大值 10。它对于除 10 之外的其他值都很好。

我用警报进行了调试,然后我发现控件根本没有进入 if 条件,即使 index[k] > max

function findTop() {
var cars = ["Hyundai", "Maruti Suzuki", "Honda", "Chevrolet", "Tata"];
var index = [cars.length];
for (var i = 0; i < cars.length; i++) {
var list = document.getElementsByName(cars[i]);

for (var j = 0; j < 10; j++) {

if (list[j].checked == true) {
index[i] = list[j].value;
}
}
}

var max = 0;
var maxIndex = 0;
var text = "";
for (var k = 0; k < index.length; k++) {
alert(index[k] + " &nbsp; " + max);
if (index[k] >= max) {
alert(index[k] + " &nbsp; " + max);
max = index[k];
alert("max" + max);
maxIndex = k;
}
}

for (var l = 0; l < 5; l++) {
if (max == index[l]) {
text = text + "&nbsp" + cars[l];
}
}

document.getElementById("output").innerHTML = text + "&nbsp&nbsp&nbsp" + max;
}
<form>
<table border="1px" width="100%">
<tr>
<td></td>
<td align="center">1</td>
<td align="center">2</td>
<td align="center">3</td>
<td align="center">4</td>
<td align="center">5</td>
<td align="center">6</td>
<td align="center">7</td>
<td align="center">8</td>
<td align="center">9</td>
<td align="center">10</td>
</tr>
<tr>
<td align="center">Hyundai</td>
<td align="center">
<input type="radio" name="Hyundai" value="1">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="2">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="3">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="4">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="5">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="6">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="7">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="8">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="9">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="10">
</td>
</tr>
<tr>
<td align="center">Maruti Suzuki</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="1">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="2">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="3">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="4">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="5">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="6">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="7">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="8">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="9">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="10">
</td>
</tr>
<tr>
<td align="center">Honda</td>
<td align="center">
<input type="radio" name="Honda" value="1">
</td>
<td align="center">
<input type="radio" name="Honda" value="2">
</td>
<td align="center">
<input type="radio" name="Honda" value="3">
</td>
<td align="center">
<input type="radio" name="Honda" value="4">
</td>
<td align="center">
<input type="radio" name="Honda" value="5">
</td>
<td align="center">
<input type="radio" name="Honda" value="6">
</td>
<td align="center">
<input type="radio" name="Honda" value="7">
</td>
<td align="center">
<input type="radio" name="Honda" value="8">
</td>
<td align="center">
<input type="radio" name="Honda" value="9">
</td>
<td align="center">
<input type="radio" name="Honda" value="10">
</td>
</tr>
<tr>
<td align="center">Chevrolet</td>
<td align="center">
<input type="radio" name="Chevrolet" value="1">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="2">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="3">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="4">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="5">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="6">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="7">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="8">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="9">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="10">
</td>
</tr>
<tr>
<td align="center">Tata</td>
<td align="center">
<input type="radio" name="Tata" value="1">
</td>
<td align="center">
<input type="radio" name="Tata" value="2">
</td>
<td align="center">
<input type="radio" name="Tata" value="3">
</td>
<td align="center">
<input type="radio" name="Tata" value="4">
</td>
<td align="center">
<input type="radio" name="Tata" value="5">
</td>
<td align="center">
<input type="radio" name="Tata" value="6">
</td>
<td align="center">
<input type="radio" name="Tata" value="7">
</td>
<td align="center">
<input type="radio" name="Tata" value="8">
</td>
<td align="center">
<input type="radio" name="Tata" value="9">
</td>
<td align="center">
<input type="radio" name="Tata" value="10">
</td>
</tr>
<tr>
<td colspan="11" align="center">
<input type="button" value="Top" onclick="findTop()">
</td>
</tr>
</table>
</form>
<p id="output"></p>

抱歉,我不知道如何添加 Jsfiddle 链接?有什么建议我做错了什么吗?

最佳答案

你的值都是字符串; “10”(字符串)永远不会大于字符串比较值“9”等。

您需要确保您的比较是数字,您可以这样做:

if (+index[k] >= max) {

function findTop() {
var cars = ["Hyundai", "Maruti Suzuki", "Honda", "Chevrolet", "Tata"];
var index = [cars.length];
for (var i = 0; i < cars.length; i++) {
var list = document.getElementsByName(cars[i]);

for (var j = 0; j < 10; j++) {

if (list[j].checked == true) {
index[i] = list[j].value;
}
}
}

var max = 0;
var maxIndex = 0;
var text = "";
for (var k = 0; k < index.length; k++) {
console.log(index[k] + " " + max);
console.log(typeof index[k]);
if (+index[k] >= max) {
console.log(index[k] + " " + max);
max = index[k];
console.log("max " + max);
maxIndex = k;
}
}

for (var l = 0; l < 5; l++) {
if (max == index[l]) {
text = text + "&nbsp" + cars[l];
}
}

document.getElementById("output").innerHTML = text + "&nbsp&nbsp&nbsp" + max;
}
<form>
<table border="1px" width="100%">
<tr>
<td></td>
<td align="center">1</td>
<td align="center">2</td>
<td align="center">3</td>
<td align="center">4</td>
<td align="center">5</td>
<td align="center">6</td>
<td align="center">7</td>
<td align="center">8</td>
<td align="center">9</td>
<td align="center">10</td>
</tr>
<tr>
<td align="center">Hyundai</td>
<td align="center">
<input type="radio" name="Hyundai" value="1">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="2">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="3">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="4">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="5">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="6">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="7">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="8">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="9">
</td>
<td align="center">
<input type="radio" name="Hyundai" value="10">
</td>
</tr>
<tr>
<td align="center">Maruti Suzuki</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="1">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="2">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="3">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="4">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="5">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="6">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="7">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="8">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="9">
</td>
<td align="center">
<input type="radio" name="Maruti Suzuki" value="10">
</td>
</tr>
<tr>
<td align="center">Honda</td>
<td align="center">
<input type="radio" name="Honda" value="1">
</td>
<td align="center">
<input type="radio" name="Honda" value="2">
</td>
<td align="center">
<input type="radio" name="Honda" value="3">
</td>
<td align="center">
<input type="radio" name="Honda" value="4">
</td>
<td align="center">
<input type="radio" name="Honda" value="5">
</td>
<td align="center">
<input type="radio" name="Honda" value="6">
</td>
<td align="center">
<input type="radio" name="Honda" value="7">
</td>
<td align="center">
<input type="radio" name="Honda" value="8">
</td>
<td align="center">
<input type="radio" name="Honda" value="9">
</td>
<td align="center">
<input type="radio" name="Honda" value="10">
</td>
</tr>
<tr>
<td align="center">Chevrolet</td>
<td align="center">
<input type="radio" name="Chevrolet" value="1">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="2">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="3">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="4">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="5">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="6">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="7">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="8">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="9">
</td>
<td align="center">
<input type="radio" name="Chevrolet" value="10">
</td>
</tr>
<tr>
<td align="center">Tata</td>
<td align="center">
<input type="radio" name="Tata" value="1">
</td>
<td align="center">
<input type="radio" name="Tata" value="2">
</td>
<td align="center">
<input type="radio" name="Tata" value="3">
</td>
<td align="center">
<input type="radio" name="Tata" value="4">
</td>
<td align="center">
<input type="radio" name="Tata" value="5">
</td>
<td align="center">
<input type="radio" name="Tata" value="6">
</td>
<td align="center">
<input type="radio" name="Tata" value="7">
</td>
<td align="center">
<input type="radio" name="Tata" value="8">
</td>
<td align="center">
<input type="radio" name="Tata" value="9">
</td>
<td align="center">
<input type="radio" name="Tata" value="10">
</td>
</tr>
<tr>
<td colspan="11" align="center">
<input type="button" value="Top" onclick="findTop()">
</td>
</tr>
</table>
</form>
<p id="output"></p>

关于JavaScript 比较 if 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31295991/

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