B
blankito1998
Beta
Verificado por Whatsapp
¡Usuario con pocos negocios! ¡Utiliza siempre saldo de Forobeta!
echo '<!DOCTYPE html>El error "Uncaught ReferenceError: inputValue is not defined" sugiere que estás intentando utilizarinputValuefuera del contexto en el que fue definido. Asegurémonos de que todas las variables se usen correctamente. Aquí tienes una estrategia para hacerlo:
### Código Revisitado
Asegúrate de que el código esté estructurado correctamente. Aquí hay una versión revisada:
JavaScript:$(document).ready(function () { var timeout; // Variable para el debounce $(".nawere").on("input", function () { clearTimeout(timeout); // Limpiar el timeout anterior var inputValue = $(this).val(); // Obtener el valor actual del input // Verificar si el input está vacío (o solo espacios) if (inputValue.trim() === "") { $(this).next(".intercomu").remove(); return; } // Buscar coincidencias de "@" seguido de caracteres alfanuméricos var matches = inputValue.match(/@\w+/g); // Si no hay coincidencias o el input termina con un espacio if (!matches || matches.length === 0 || inputValue.endsWith(" ")) { $(this).next(".intercomu").remove(); return; } // Usar debounce para retrasar la ejecución de la búsqueda timeout = setTimeout(() => { $(this).next(".intercomu").remove(); // Eliminar el intercomu anterior // Realizar el AJAX para buscar datos $.get("../hola.php", { searpro: matches[matches.length - 1] }, function (data) { $("<span class='intercomu'>Estás buscando " + matches[matches.length - 1] + ": " + data + "</span>") .insertAfter($(this)); // Inserta después del input actual }.bind(this)); // Mantiene el contexto del input actual }, 300); // Ajusta el tiempo según sea necesario }); // Cuando el input pierde el foco, se elimina el intercomu $(".nawere").on("blur", function () { $(this).next(".intercomu").remove(); // Elimina el intercomu asociado }); });
### Aspectos Clave
1. Definición deinputValue:
- Asegúrate de queinputValuesolo se utiliza dentro del bloque de código donde se ha definido. No intentes acceder ainputValuefuera del evento de entrada.
2. Consola de Desarrollo:
- Usaconsole.log(inputValue)justo después de definir la variable para asegurarte de que se captura correctamente.
3. HTML:
- Verifica que el elemento con la clasenawereesté presente en tu HTML y que no haya errores que eviten que el jQuery se ejecute correctamente.
4. Errores de Sintaxis:
- Asegúrate de que no haya errores tipográficos en el código que podrían impedir que el script funcione correctamente.
### Depuración
Agrega estas líneas para depurar el comportamiento:
JavaScript:console.log("Input Value: ", inputValue); console.log("Matches: ", matches);
Si el error persiste, verifica también en la consola del navegador si hay otros errores de JavaScript que puedan estar afectando la ejecución del código. Si necesitas más ayuda, no dudes en compartir más detalles sobre cómo se está configurando tu HTML. ¡Estoy aquí para ayudar!
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style>
.intercomu{
box-shadow:0px 0px 3px #000;
background:#ddd;
padding:7px;
width:200px;
float:left;
}
.lobre{
box-sizing:border-box;
background:#ccc;
cursor😛ointer;
padding:7px;
width:100%;
float:left;
}
.lobre:hover{
background:gray;
}
</style>
</head>
<body>
<textarea class="nawere" dataid="1"></textarea>
';
?>
<script>
$(document).ready(function () {
var timeout; // Variable para el debounce
$(".nawere").on("input", function () {
clearTimeout(timeout); // Limpiar el timeout anterior
var inputValue = $(this).val(); // Obtener el valor actual del input
// Verificar si el input está vacío (o solo espacios)
if (inputValue.trim() === "") {
$(this).next(".intercomu").remove();
return;
}
// Buscar coincidencias de "@" seguido de caracteres alfanuméricos
var matches = inputValue.match(/@\w+/g);
// Si no hay coincidencias o el input termina con un espacio
if (!matches || matches.length === 0 || inputValue.endsWith(" ")) {
$(this).next(".intercomu").remove();
return;
}
// Usar debounce para retrasar la ejecución de la búsqueda
timeout = setTimeout(() => {
$(this).next(".intercomu").remove(); // Eliminar el intercomu anterior
// Realizar el AJAX para buscar datos
$.get("../hola.php", { searpro: matches[matches.length - 1] }, function (data) {
$("<span class='intercomu'>Estás buscando " + matches[matches.length - 1] + ": " + data + "</span>")
.insertAfter($(this)); // Inserta después del input actual
}.bind(this)); // Mantiene el contexto del input actual
}, 300); // Ajusta el tiempo según sea necesario
});
// Cuando el input pierde el foco, se elimina el intercomu
$(".nawere").on("blur", function () {
$(this).next(".intercomu").remove(); // Elimina el intercomu asociado
});
});
console.log("Input Value: ", inputValue);
console.log("Matches: ", matches);
</script>
<?php echo '
</body>
</html>';
?>
Uncaught ReferenceError: inputValue is not defined
at tete.php:68:30