简体   繁体   English

使用eq的jQuery单选按钮设置失败

[英]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. 在下面的代码中, plane.posFile是一个长度为3的字符串数组,用于标识3个单选按钮,并且这些按钮可以正确显示。

If oSource.posfile is a blank string, the 0th button gets checked correctly. 如果oSource.posfile是空字符串,则正确检查第0个按钮。

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. 如果oSource.posfile不为空,则它是plane.posFile中的三个文件之一,只是为了进行比较必须删除文件扩展名。

What happens is that the code correctly (using Firebug to step through it) chooses which index to use. 发生的事情是,正确的代码(使用Firebug逐步执行)选择了要使用的索引。

It goes through into the break statement and then out with i=1 . 它进入break语句,然后以i=1

However, the attempt to set when eq(i) the attr('checked',true) files. 但是,尝试设置eq(i) attr('checked',true)文件的时间。 I don't see why. 我不明白为什么。 It's the same statement as previously when eq(0) was used. 与使用eq(0)时的语句相同。 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);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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