HTML-encoding lost when attribute read from input field

前端 未结 25 3810
时光说笑
时光说笑 2020-11-21 04:04

I’m using JavaScript to pull a value out from a hidden field and display it in a textbox. The value in the hidden field is encoded.

For example,



        
25条回答
  •  无人共我
    2020-11-21 05:01

    I had a similar problem and solve it using the function encodeURIComponent from JavaScript (documentation)

    For example, in your case if you use:

    
    

    and

    encodeURIComponent($('#hiddenId').attr('value'))
    

    you will get chalk%20%26%20cheese. Even spaces are kept.

    In my case, I had to encode one backslash and this code works perfectly

    encodeURIComponent('name/surname')
    

    and I got name%2Fsurname

提交回复
热议问题