代码在for循环之后没有执行--javascript

时间:2015-08-06 03:17:33

标签: javascript

我正在编写一个应该循环遍历所有复选框的函数并加载"值"检查到数组的复选框,但是没有任何执行超出第一个for循环。我完全被这里难住了,我错过了什么?

public static char getCheckSum(String isbn) {
    int sum = 0;

    for (int i = 0; i < isbn.length(); i++) {
        int[] num = new int[isbn.length()]; 
        num[i] = Character.getNumericValue(isbn.charAt(i));
        sum = sum + num[i];
    }
    int last = (sum % 11);
    char charSum;
    if (last == 10){
        charSum = 'X';
    } else {
        charSum = (char) (last + 48);
    }
    return charSum;
}

public static String formatISBNWithHyphens(String isbn) {
    // original isbn:       123456789
    // possible new isbn:   1-234-56789-X
    char isbn10 = getCheckSum(isbn);
    String isbn10Str = isbn + Character.toString(isbn10);

//  char[] c = new char[isbn10Str.length()];    *leaving this here for future learning.
    String[] cStr = new String[isbn10Str.length()];
    String isbnStr = "";
    for (int i = 0; i < isbn10Str.length(); i++){
        cStr[i] = Character.toString(isbn10Str.charAt(i));
//      c[i] = isbn10Str.charAt(i);             *leaving this here for future learning.
        if (i == 0 || i == 3 || i == 8 ) {
            cStr[i] += '-';
         }

         isbnStr += cStr[i];
    }
    return isbnStr;
}

4 个答案:

答案 0 :(得分:0)

在这一行

if(document.getElementById("check"+i).checked == true){ //...

getElementById将返回 undefined 值,而undefined不具有.checked属性。

您在文档中有多少个复选框?

答案 1 :(得分:0)

尝试以下工作解决方案。代码中的第二个循环条件(i == checked.length)是错误的。它应该是@echo off java -Xms512M -Xmx512M -jar C:\Users\admin\workspace\housekeep\housekeep.jar -o true PAUSE

i < checked.length)

答案 2 :(得分:0)

你的第二个for循环有一个错误

<s:iterator id="n" value="news">
<tr>
    <td>
    <s:url action="GETNEWS" var="getNews">
        <s:a href="%{getNews}">
            <s:property value="#n.title" />
            <s:hidden name="id" value="#n.id" />
        </s:a>
    </s:url>
    </td>
</tr>
</s:iterator>
function saveSettings(){
    var count = 1; //count for db
    var checked=[]; //array of checked values
    var inc = 0; //only incremented when checkbox is checked
    var dataString = "x";

    for(var i=0; i<=2; i++) { //loads checked array with checked values
        if(document.getElementById("check"+i).checked == true){
            checked[inc] = document.getElementById("check"+i).value;
            alert(checked[inc]) // <------ executing as expected
            inc++;
        }
    }

    alert("made it") // <------ not executing
    if(checked.length>0){ // loads checked values into dataString
        for(var i=0; i < checked.length; i++){
            if(i === 0){
                dataString = "co_" + count +"="+checked[i]
            }
            else {
                dataString = dataString +"&co_" + count +"="+checked[i]
            }
            count++;
        }
    }

    alert(dataString)
    }

答案 3 :(得分:0)

根据您对问题的描述,我认为问题是复选框的数量,如果您只有2个复选框,那么条件i<=2将导致问题,因为docuement.getElementById('check2')将是未定义的,从而导致错误

。{/ p>

由于您正在尝试处理动态数量的元素,请尝试使用类来选择感兴趣的复选框,如

&#13;
&#13;
Uncaught TypeError: Cannot read property 'checked' of null
&#13;
function saveSettings() {

  var checked = [];
  var dataString = "x";

  var checks = document.getElementsByClassName('check');
  //if you can't add a class then fetch all the checkboxes in the page
  //var checks = document.querySelectorAll('input[type="checkbox"]');

  for (var i = 0; i < checks.length; i++) {
    if (checks[i].checked == true) {
      checked.push(checks[i].value);
    }
  }
  console.log("made it: ", checked)

  if (checked.length > 0) {
    dataString = '';
    for (var i = 0; i < checked.length; i++) {
      dataString += (i == 0 ? '' : '&') + "co_" + i + "=" + checked[i]
    }
  }
  alert(dataString)
}
&#13;
&#13;
&#13;