Al utilizar la funcion replace()
me sale este error. La estoy utilizando porque estoy generando codigo html con valores introducidos del usuario con un boton.
El problema es que si el usuario introduce comillas dobles (") o simple (') no se genera el codigo correctamente.
Con la funcion replacre("'", "``")
puedo remplazar esas comillas simples
pero me sale este error
backoffice.js:128 Uncaught TypeError: Cannot read property 'replace' of null(…)
Inserto la parte del codigo a ver si veis el error.
function modificarSugerencia(id){
$.get("modules/backoffice/sugerencias/leer_sugerencia.php?idSugerencia=" + id, function(data){
if(data)
{
json = JSON.parse(data);
//console.log(data);
console.log(json.sugerencia.CUERPO_SUGERENCIA);
url="modules/backoffice/sugerencias/rellenadoSugerencias.php";
urlReturn="modules/backoffice/sugerencias/gestion_sugerencias.php";
var boxhtml ="";
boxhtml+="<form action='' name='comunicationUpdate' id='comunicationUpdate' method='post'";
boxhtml+="<div id='box-modificar-comunicado' class='capa-fondo-caja capa-fondo-caja-backoffice centered-absolute'>";
boxhtml+="<div class='header-caja-fondo-backoffice'>"
boxhtml+="<h2><span class='icon-document-edit size-icons-table-backoffice'></span>Modificación sugerencia</h2>";
boxhtml+="</div>";
boxhtml+="<div id='form-main'>";
boxhtml+="<p class='asunto'>Asunto:";
boxhtml+="<input name='asunto' type='text' class='feedback-input' placeholder='Asunto' id='asunto' value='"+json.sugerencia.ASUNTO.replace("'","`")+"' />";
boxhtml+="</p>";
boxhtml+="<p class='fechas'>Descripcion mejora:";
boxhtml+="<input name='cuerpoSugerencia' type='text' class='feedback-input' value='"+json.sugerencia.CUERPO_SUGERENCIA.replace("'","`")+"' />";
boxhtml+="</p>";
boxhtml+="<p class='text'>Respuesta:";
boxhtml+="<input name='respuesta' type='text' class='feedback-input' id='comment' value='"+json.sugerencia.RESPUESTA.replace("'","`")+"' placeholder='Comunicado'>";
boxhtml+="</p>";
Чтобы предотвращать ошибки с данными null
ты должен проверять
Javascript:
if ( typeof(json.sugerencia.ASUNTO) !== "undefined" && json.sugerencia.ASUNTO !== null ) {
boxhtml+="<div id='form-main'>";
boxhtml+="<p class='asunto'>Asunto:";
boxhtml+="<input name='asunto' type='text' class='feedback-input' placeholder='Asunto' id='asunto' value='"+json.sugerencia.ASUNTO.replace("'","`")+"' />";
boxhtml+="</p>";
}else {
//Error
}
Чтобы убегать простые кавычки или двойной дебет использовать \
. Эдж:
replace("\'","´´")
Cannot read property 'replace' of null(…)
. Ademá s, ¿ из-за qué serí в необходимый убегать простые кавычки в твоем примере?
– Mariano
21.12.2016, 13:17