숨겨진 필드 속성을 읽을 때 HTML 인코딩 보존
인코딩된 숨겨진 입력 필드에서 값을 추출할 때 JavaScript는 프로세스 중에 인코딩을 잃는 경우가 많습니다. . 이는 리터럴 앰퍼샌드(&) 문자를 유지하려는 의도인 경우 문제가 됩니다.
해결책:
이 문제를 해결하기 위해 JavaScript는 HTML로 인코딩할 수 있는 메서드를 제공합니다. 문자열. 이러한 방법 중 하나는 다음과 같습니다.
function htmlEncode(value) { // Create an in-memory textarea element and set its inner text. // Text within a textarea is automatically encoded. return $('<textarea/>').text(value).html(); }
사용법:
이 기능을 코드에 통합하여 숨겨진 필드에서 읽을 때 HTML 인코딩이 유지되도록 할 수 있습니다.
var encodedValue = $('#hiddenId').attr('value'); var decodedValue = htmlDecode(encodedValue);
또는 jQuery 라이브러리는 htmlDecode()라는 메서드를 제공합니다. 인코딩:
// Decoding the encoded value var decodedValue = decodedValue = $('<div/>').html(encodedValue).text();
예:
숨겨진 필드 고려:
<input>
htmlEncode() 함수 사용:
var encodedValue = $('#hiddenId').attr('value'); console.log(encodedValue); // Outputs: chalk & cheese
htmlDecode() 사용 방법:
var decodedValue = $('<div/>').html(encodedValue).text(); console.log(decodedValue); // Outputs: chalk &cheese
두 경우 모두 추출 과정에서 원본 HTML 인코딩이 유지됩니다.
위 내용은 JavaScript의 숨겨진 입력 필드에서 값을 검색할 때 HTML 인코딩을 어떻게 보존할 수 있습니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!