如果未输入文本,则保持按钮禁用 [英] Keep button disabled if text is not entered

查看:140
本文介绍了如果未输入文本,则保持按钮禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现这样的目标. 我有4个文本字段和一个选择字段,这些都是表单提交所必需的.我想让按钮保持禁用状态,除非文本字段具有值并且从下拉列表中选择了一个选项.我的代码是此处

I want to achieve something like this. I have 4 text fields and one select field that are required for the form to submit. I want to keep the button disabled unless the text field has values and an option is selected from the dropdown. My code is here

我遇到的问题是,每当我在4个文本字段中输入数据时,尽管将下拉列表选择为默认文本,但该按钮仍处于启用状态.更改下拉列表后,它将永远被禁用.

The problem I have is whenever I enter the data in the 4 text fields and though the dropdown is selected to the default text the button gets enabled. After I change the dropdown it is disabled forever.

推荐答案

尝试一下- 演示

Try this - DEMO

$('#fname, #lname, #zipcode').on("keyup", action);
$('#location').on("change", action);

function action() {
    if( $('#fname').val().length > 0 && $('#lname').val().length > 0 && $('#zipcode').val().length > 0 && $('#location').val() != '' ) {
        $('#submit').prop("disabled", false);
    } else {
        $('#submit').prop("disabled", true);
    }   
}

这篇关于如果未输入文本,则保持按钮禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆