简体   繁体   中英

jQuery setting of radio button using eq fails

In the code below, plane.posFile is a string array of length 3 that identifies 3 radio buttons, and the buttons display correctly.

If oSource.posfile is a blank string, the 0th button gets checked correctly.

If oSource.posfile is not blank, it is one of the three in plane.posFile except that the file extensions have to be stripped for the comparison.

What happens is that the code correctly (using Firebug to step through it) chooses which index to use.

It goes through into the break statement and then out with i=1 .

However, the attempt to set when eq(i) the attr('checked',true) files. I don't see why. It's the same statement as previously when eq(0) was used. I've been staring at it for a couple of hours trying things with no luck.

Any ideas are greatly appreciated, including comments on the approach.

$wb.pcPosCfg.empty();
for (i=0; i<plane.posFile.length; i++)
     $wb.pcPosCfg.append("<input type='radio' style='width:11px' name='rbnLoadCfg' value=" + i + ">" + plane.posFile[i] + "<br>");
    if (oSource.posfile=='') {
        $('input[name="rbnLoadCfg"]:eq(0)').attr('checked',true);
    }
    else {
        for (i=0; i<plane.posFile.length; i++) {
            if (oSource.posfile==plane.posFile[i].substring(0,plane.posFile[i].length-5))
                break;
            }
        $('input[name="rbnLoadCfg"]:eq(i)').attr('checked',true);
    }
}
$('input[name="rbnLoadCfg"]:eq('+i+')').attr('checked',true);

您需要连接字符串以正确包含变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM