简体   繁体   English

添加一些显示不同长度的特殊字符时的Javascript数组

[英]Javascript array when adding some special characters showing different length

In my jsp page I'm having some javascript code,to take all the values that are entered in textboxes. 在我的jsp页面中,我有一些JavaScript代码,以获取在文本框中输入的所有值。 And i'm putting those in array then passing to next page. 我将它们放在数组中,然后传递到下一页。 This is my javascript function 这是我的JavaScript函数

function gt2()
{
    var pqr="100";
    var arr=new Array();
    var x=<%=height%>;
    var attstr=null;
    for(var t=0; t<x; t++)
    {
        var a="inputText"+t;

        var e=document.getElementById(a);
        var val= e.value;

        arr[t]=val;

        if(t==0)
        {
            attstr=arr[t]+",";
        }

        if((t!=x)&&(t!=0))
        {
            if(t==x-1)
            {
                attstr+=arr[t];
            }
            else
            {
                attstr+=arr[t]+",";
            }
        }
    }
    window.location.assign("gt_Iba2?value="+attstr);
}    

In the second jsp page I'm getting that value. 在第二个jsp页面中,我得到了该值。

String newValue=request.getParameter("value");
System.out.println("!###The new value in second jsp is "+newValue);
String[] allvalues=newValue.split(",");
System.out.println("The length of allvalues is "+allvalues.length);

I have five textboxes in my first jsp. 我的第一个jsp中有五个文本框。 My code is correctly picking the values from those textboxes and passing to other page only if it doesn't contain some special characters. 我的代码正确地从这些文本框中选择值,并仅在其中不包含某些特殊字符的情况下才传递到其他页面。 img1

Output for above condition is fine for me. 上述条件的输出对我来说很好。

!###The new value in second jsp is A|B|C|Dbcdrt,A|B|C|Dbcdrt,A|B|C|Dbcdrt,A|B|C|Dbcdrt,A|B|C|Dbcdrt
The length of allvalues is 5

So when I'm adding any characters like #,&,% the array is breaking.Means it is not taking other values after that character. 因此,当我添加#,&,%之类的字符时,数组中断了,这意味着该字符之后没有其他值。

In this case 在这种情况下 img2

I have added '&' symbol on third textbox. 我在第三个文本框中添加了“&”符号。 So I'm getting output like this 所以我得到这样的输出

!###The new value in second jsp is A|B|C|Dbcdrtdf,A|B|C|Dbcdrtdf,A|B|C|Dbcdrtdf 
The length of allvalues is 3

After that '&' symbol it is not showing any value. 在“&”符号之后,它没有显示任何值。 The size is also three.If I add this in second text box the size will be two.Why this behaviour on these symbols?I need to pass those symbols also.What wrong am I doing here?I need some help 大小也为三,如果我在第二个文本框中添加该大小,将为二。为什么在这些符号上有这种行为?我也需要传递这些符号。我在这里做错什么了?我需要一些帮助

Thanks 谢谢

The & symbol seperates the get parameters in the url ( parameter1=value&parameter2=value2 ). &符号用于分隔url中的get参数( parameter1=value&parameter2=value2 )。 So everything after the symbol is treated as a second parameter. 因此,将符号后的所有内容都视为第二个参数。

If you use the javascript function encodeURIComponent to encode attrstr then it should work. 如果您使用JavaScript函数encodeURIComponent编码attrstr则它应该可以工作。

The problem you have is that the characters #&%? 您遇到的问题是字符#&%? are all reserved characters when used as part of a URI/URL. 当用作URI / URL的一部分时,都是保留字符。 You should use: 您应该使用:

var val= encodeURIComponent (e.value);

this will URI escape those characters and the URI will remain legal. URI将转义这些字符,并且URI将保持合法。

Personally I would use Array.join to concatenate your array with commas too. 就我个人而言,我也将使用Array.join将您的数组与逗号连接起来。

    function gt2()
    {
        var pqr="100";
        var arr=new Array();
        var x=<%=height%>;

        for(var t=0;t<x;t++)
        {
            var a="inputText"+t;

            var e=document.getElementById(a);
            var val= encodeURIComponent (e.value);

            arr[t]=val;
        }

        var attrstr = arr.join(',');

        window.location.assign("gt_Iba2?value="+attstr);
    }

PS In your loop (t!=x) will always be true. PS在您的循环中(t!=x)将始终为true。

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

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