最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Invalid String LengthAllocation Size Overload JavaScript - Stack Overflow

programmeradmin4浏览0评论

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 mas. 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 e 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.

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 mas. 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 e 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.

Share Improve this question edited Aug 30, 2014 at 21:45 Michael Megee asked Aug 30, 2014 at 21:38 Michael MegeeMichael Megee 351 silver badge5 bronze badges 7
  • I would try interests = [] instead of new Array() – Adem İlhan Commented Aug 30, 2014 at 21:42
  • The code seems to be correct. It may be related to your rest of code. Please check your rest of code too. – nisargjhaveri Commented Aug 30, 2014 at 21:45
  • should always declare variables using var – charlietfl Commented Aug 30, 2014 at 21:47
  • For some reason, [] instead of new Array() got rid of the error. But new Array() is the proper syntax, is it not? Thank you, though. I appreciate the help. – Michael Megee Commented Aug 30, 2014 at 21:47
  • With array literals, [] is actually a little faster. All modern JS VMs understand that [] means "new array literal". When you use "new Array()," it takes a couple extra cycles because the Array constructor is overloaded. In short-- you should use [], not new Array(). I found a great old answer on this here. – Antiga Commented Aug 30, 2014 at 21:51
 |  Show 2 more ments

1 Answer 1

Reset to default 5
if ( i = interests.length - 2 ) interestsString += "and ";

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

发布评论

评论列表(0)

  1. 暂无评论