Pregunta

I have a little script here that should enable/disable a button based on whether or not either of two inputs are blank. The button starts out disabled. When I enter a character into either input, the button is enabled. But when I delete the characters from the inputs, the button doesn't disable again. Any idea what I'm doing wrong?

<input type="text" class="adults" name="adults" />
<input type="text" class="children" name="children" />
<button type="submit" class="calculate" disabled="disabled">Calculate</button>
<script>            
    jQuery("input").keyup(function() {
        if ((jQuery("input.adults").val() !== undefined) || (jQuery("input.children").val() !== undefined)) jQuery(".calculate").removeAttr("disabled");
        else jQuery(".calculate").attr("disabled", "disabled");
    });
</script>
¿Fue útil?

Solución

Instead of using undefined, use empty "" string.

if ((jQuery("input.adults").val() !== "") || (jQuery("input.children").val() !== ""))
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top