JavaScript比较如果不起作用?

时间:2015-07-08 14:41:11

标签: javascript html

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

我使用警报进行了调试,然后我发现即使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链接?有什么建议我做错了吗?

1 个答案:

答案 0 :(得分:3)

你的价值观都是字符串; "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>