- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码来检查汽车的最大值。但是每当我在表中选择 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] + " " + max);
if (index[k] >= max) {
alert(index[k] + " " + max);
max = index[k];
alert("max" + max);
maxIndex = k;
}
}
for (var l = 0; l < 5; l++) {
if (max == index[l]) {
text = text + " " + cars[l];
}
}
document.getElementById("output").innerHTML = text + "   " + 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 + " " + cars[l];
}
}
document.getElementById("output").innerHTML = text + "   " + 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/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!