Estoy creando un sitio web. Quiero que cuando se ingrese un texto específico, en y <input> , y luego se envíe con <button> , javascript avise a la página y responda con algo específico. Ej: si la entrada ingresada es $ip , la función de alert() de js enviará su ip, o si hace $add 2 2 , alertará 2+2=4 .
html
<form> <input class="text" id="txt"> <button id="btn" class="submit-btn" onclick="get()">submit</button> </form>JS
function get() { var inputVal = document.getElementById("txt").value; alert(inputVal); }; };Puede leer el valor de entrada cuando el formulario se ha enviado y hacer lo que quiera con el valor. como esto:
function handleSubmit(event) { event.preventDefault(); const value = document.getElementById('txt').value.trim(); switch (value) { case '': break; case '$ip': alert('You IP:xxx'); break; case '$add 2 2': alert('2+2=4'); break; default: alert(value + ': is not a valid action'); } } <form onsubmit="handleSubmit(event)"> <input class="text" id="txt"> <button id="btn" class="submit-btn">submit</button> </form>