HTML-encoding lost when attribute read from input field

前端 未结 25 3904
时光说笑
时光说笑 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:06

    You shouldn't have to escape/encode values in order to shuttle them from one input field to another.

    JS doesn't go inserting raw HTML or anything; it just tells the DOM to set the value property (or attribute; not sure). Either way, the DOM handles any encoding issues for you. Unless you're doing something odd like using document.write or eval, HTML-encoding will be effectively transparent.

    If you're talking about generating a new textbox to hold the result...it's still as easy. Just pass the static part of the HTML to jQuery, and then set the rest of the properties/attributes on the object it returns to you.

    $box = $('').val($('#hiddenId').val());
    

提交回复
热议问题