无效的字符串长度/分配大小超载的JavaScript [英] Invalid String Length/Allocation Size Overload JavaScript

查看:215
本文介绍了无效的字符串长度/分配大小超载的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列的复选框,检查,他们已经得到遏制,如果他们有一个字符串添加到一个数组。然后,我遍历字符串数组,数组中添加字符串用逗号分隔的一个新的字符串。不幸的是,结果总是在错误。在Chrome中,控制台上写着无效的字符串长度。在Firefox中,错误显示为分配大小溢出。

JavaScript的:

 利益//编译名单
权益=新的Array();
如果($(输入[名称= interestRoofing])是(':检查')== true)而interests.push(屋顶);
如果($(输入[名称= interestSiding])是(':检查')== true)而interests.push(死盯);
如果($(输入[名称= interestGutters])是(':检查')== true)而interests.push(排水沟);
如果($(输入[名称= interestWindows])是(':检查')== true)而interests.push(视窗);
如果($(输入[名称= interestPaint])是(':检查')== true)而interests.push(外墙漆);
interestsString =;
对于(i = 0; I< interests.length;我++){//无效的字符串长度在Chrome在这条线上
    interestsString = interestsString +利益[I] //分配大小过载在Firefox在这条线上
    如果(ⅰ&下; interests.length - 2)interestsString + =;
    如果(ⅰ= interests.length - 2)interestsString + =及;
}

HTML

 < P>感兴趣的:< / P>
< P><输入类型=复选框NAME =interestRoofingVALUE =>屋面及LT; / P>
< P><输入类型=复选框NAME =interestSidingVALUE =>屋面< / P>
< P><输入类型=复选框NAME =interestGuttersVALUE =>排水沟< / P>
< P><输入类型=复选框NAME =interestWindowsVALUE => Windows和LT; / P>
< P><输入类型=复选框NAME =interestPaintVALUE =>外墙漆< / P>

我敢肯定,无论是走错了只会打我时,我看到它的脸,但我已经整天坐在在这个问题上并没有拿出一个解决办法呢。显然,有一些东西在这里,我没有看到。任何帮助将是非常美联社preciated。奇怪的是,在Internet Explorer中的控制台不吐什么的,好像什么都没有出错。
我可以通过添加一个if语句去掉无效的字符串长度错误的Chrome浏览器的开始循环检查,以确保该数组的长度不是0。但是,Firefox中的错误仍然存​​在,因此必须不是解决办法


解决方案

 如果(I = interests.length  -  2)interestsString + =和;

您在这里做一个赋值。你可能都指被比较。

I have a series of checkboxes, check to see that they've been checked, and if they have, add a string to an array. Then I loop through the array of strings and add the strings in the array to a new string separated by commas. Unfortunately, this always results in errors. In Chrome, the console reads "Invalid String Length". In Firefox, the error reads "allocation size overflow".

JavaScript:

// Compile List of Interests
interests = new Array();
if ( $("input[name=interestRoofing]").is(':checked') == true ) interests.push("Roofing");
if ( $("input[name=interestSiding]").is(':checked') == true ) interests.push("Siding");
if ( $("input[name=interestGutters]").is(':checked') == true ) interests.push("Gutters");
if ( $("input[name=interestWindows]").is(':checked') == true ) interests.push("Windows");
if ( $("input[name=interestPaint]").is(':checked') == true ) interests.push("Exterior Paint");
interestsString = "";
for ( i = 0; i < interests.length; i++ ) { // "Invalid String Length" in Chrome on This Line
    interestsString = interestsString + interests[i]; // "Allocation Size Overload" in Firefox on This Line
    if ( i < interests.length - 2 ) interestsString += ", ";
    if ( i = interests.length - 2 ) interestsString += "and ";
}

HTML:

<p>Interested In:</p>
<p><input type="checkbox" name="interestRoofing" value="">Roofing</p>
<p><input type="checkbox" name="interestSiding" value="">Siding</p>
<p><input type="checkbox" name="interestGutters" value="">Gutters</p>
<p><input type="checkbox" name="interestWindows" value="">Windows</p>
<p><input type="checkbox" name="interestPaint" value="">Exterior Paint</p>

I'm sure that whatever is going wrong will just hit me in the face when I see it, but I've been sitting on this problem all day and haven't come up with a solution yet. Obviously there's something here I'm not seeing. Any help would be much appreciated. Strangely, the console in Internet Explorer doesn't spit anything out, as if nothing went wrong. I can rid Chrome of the Invalid String Length error by adding an if statement to the beginning of the for loop checking to ensure that the length of the array is not 0. However, the error in Firefox remains, so that must not be the solution.

解决方案

if ( i = interests.length - 2 ) interestsString += "and ";

You are doing an assignment here. You probably mean to be comparing.

这篇关于无效的字符串长度/分配大小超载的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆