B
blankito1998
Beta
¡Usuario con pocos negocios! ¡Utiliza siempre saldo de Forobeta!
$(document).ready(function() {Entiendo que el mensaje "Estás buscando @a" sigue apareciendo incluso cuando hay texto adicional después de la mención. Vamos a hacer ajustes para asegurarnos de que el intercomu no se muestre si hay texto adicional después de la mención. Aquí tienes el código modificado:
### Código PHP Ajustado
PHP:<?php session_start(); error_reporting(0); require_once('database.php'); if(!($conexion = mysqli_connect("localhost", "" . $username_progold . "", "" . $password_progold . ""))); mysqli_select_db($conexion, "" . $database_progold . ""); require_once('gnshop.php'); echo '<!DOCTYPE html> <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: pointer; 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 // Eliminar el intercomu existente $(this).next(".intercomu").remove(); // Verificar si el input está vacío o solo tiene espacios if (inputValue.trim() === "") { return; } // Buscar coincidencias de "@" seguido de caracteres alfanuméricos var matches = inputValue.match(/@\w+/g); // Si hay más de una mención, no mostrar el intercomu if (matches && matches.length > 1) { return; } // Usar debounce para retrasar la ejecución de la búsqueda timeout = setTimeout(() => { // Solo proceder si hay una mención if (matches && matches.length === 1) { var lastMention = matches[0]; // Obtener la última mención var lastMentionIndex = inputValue.lastIndexOf(lastMention); var charAfterMention = inputValue.charAt(lastMentionIndex + lastMention.length); // Mostrar el intercomu solo si el siguiente carácter es un espacio if (charAfterMention === ' ' || charAfterMention === '') { $.get("../hola.php", { searpro: lastMention }, function (data) { $("<span class='intercomu'>Estás buscando " + lastMention + ": " + 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 }); }); </script> <?php echo ' </body> </html>'; ?>
### Cambios Realizados
1. Verificación del Carácter Posterior a la Mención: Se asegura que el intercomu solo se muestre si el carácter siguiente a la mención es un espacio o si no hay más texto después de la mención.
2. Condición Simplificada: Solo se permite visualizar el intercomu si hay un solo "@" y el siguiente carácter es un espacio o se encuentra al final del texto.
### Comportamiento Esperado
Ahora, si escribes "hola como estas @a jaja espero la pases bien", el intercomu no debería mostrarse, ya que hay texto adicional después de la mención.
Prueba este código y avísame si sigue sin funcionar; estaré aquí para ayudarte a resolverlo.
$('.nawere').on('input', function() {
var inputValue = $(this).val();
var dataid = $(this).attr('dataid');
// Eliminar el intercomu existente
$(".intercomu").remove();
// Buscar coincidencias de menciones que comiencen con '@'
var matches = inputValue.match(/@(\w+)/g);
// Si hay coincidencias
if (matches) {
var lastMention = matches[matches.length - 1]; // obtener la última mención
// Verifica que el último carácter después de la última mención no sea un espacio
var lastMentionIndex = inputValue.lastIndexOf(lastMention);
if (lastMentionIndex + lastMention.length < inputValue.length && !inputValue[lastMentionIndex + lastMention.length].trim()) {
return; // Si el carácter después de la mención es un espacio, no mostramos el intercomu
}
$.get("../hola.php", { searpro: matches[matches.length - 1] }, function (data) {
$('<div class="intercomu cls' + dataid + '">Estás buscando ' + lastMention + '</div>')
.insertAfter($(this));
)};
}
});
});
como hago que funcione el $.get?

