The value of js will default the numeric string to a number, and discard the previous 0

The value of coode I got in the background is passed to another page again, this page is written in jsp.
Pseudocode for page reception:

var co=${
    
    coCode}

Then something strange happened. The result in the browser debugging interface is:

var co= 0986
但是alter(co)
页面跳出来的值是 986,
忽略了前面的 0 .

The first method is like this:

Define a hidden input tag on the JSP page and put the value in it

<input type="hidden" id="co" value=${
    
    coCode}>
然后根据id选择器来接收:
var co = $("#co");
此时alert出来的是 0986.

The second method (recommended):

var co ="${
    
    coCode}"
 el表达式加上引号(适用于jsp页面)
 alert出来的也是 0986

The third method (recommended):

<script>
    b("001");
    function b(id)
    {
    
    
        console.log("b函数的id:"+id);
        //错误代码这里id默认理解成数字类型,需要"",直接在前面添加会影响html结构报语法错误所以需要把"需要转义\"
        //var history = "<a href='#' οnclick='a(" +id + ")'>历史</a>"; 

        //正确写法
        var history = "<a href='#' οnclick='a(\"" +id + "\")'>历史</a>";
        document.write(history)
    }
    function a(id)
    {
    
    
        console.log("a函数的id:"+id);
    }
</script>

Guess you like

Origin blog.csdn.net/qq_39327418/article/details/102547258