简体   繁体   English

如果jQuery中输入和选择字段为空,如何向按钮添加禁用属性

[英]How to add disabled attribute to button if input and select fields are empty in jquery

I am trying to add a disabled attribute to the button if all input fields and all select dropdown fields are empty but I'm having no luck. 如果所有输入字段和所有选择下拉字段均为空,但是我没有运气,我试图向按钮添加禁用属性。 Here's the form: 形式如下:

<form id="ridefinancing">
<fieldset id="details">
    <h3 class="fs-title">Your Details</h3>
    <div id="div_id_firstname" class="form-group required">
        <label for="id_firstname" class="control-label col-md-3  requiredField"> Firstname<span class="asteriskField">*</span> </label>
        <div class="controls col-md-9">
            <input class="form-control gradient" id="id_firstname" maxlength="30" name="firstname" style="margin-bottom: 10px" type="text" />
        </div>
    </div>
    <div id="div_id_lastname" class="form-group required">
        <label for="id_lastname" class="control-label col-md-3  requiredField"> Lastname<span class="asteriskField">*</span> </label>
        <div class="controls col-md-9 ">
            <input class="form-control gradient" id="id_lastname" maxlength="30" name="lastname" style="margin-bottom: 10px" type="text" />
        </div>
    </div>
    <div id="div_id_email" class="form-group required">
        <label for="id_email" class="control-label col-md-3  requiredField"> Email<span class="asteriskField">*</span> </label>
        <div class="controls col-md-9">
            <input class="form-control gradient" id="id_email" maxlength="30" name="email" style="margin-bottom: 10px" type="text" />
        </div>
    </div>
    <div id="div_id_phone" class="form-group required">
        <label for="id_phone" class="control-label col-md-3  requiredField"> Phone<span class="asteriskField">*</span> </label>
        <div class="controls col-md-9 ">
            <input class="form-control gradient" id="id_phone" maxlength="30" name="phone" style="margin-bottom: 10px" type="text" />
        </div>
    </div>
    <div id="div_id_birthday" class="form-group required">
        <label for="id_birthday" class="control-label col-md-3  requiredField"> Birthday<span class="asteriskField">*</span> </label>
        <div class="controls col-md-9">
            <div class="row">
                <div class="col-xs-4">
                    <select class="form-control gradient" id="id_day">

                    </select>
                </div>
                <div class="col-xs-4">
                    <select class="form-control gradient" id="id_month">

                    </select>
                </div>
                <div class="col-xs-4">
                    <select class="form-control gradient" id="id_year">
                    </select>
                </div>
            </div>
        </div>
    </div>
    <div id="div_id_status" class="form-group required">
        <label for="id_status" class="control-label col-md-3  requiredField"> Marital Status<span class="asteriskField">*</span> </label>
        <div class="controls col-md-9">
            <select class="form-control gradient" id="id_status">
                <option>Please Select</option>
                <option>Single</option>
                <option>Married</option>
                <option>Divorced</option>
            </select>
        </div>
    </div>
    <div id="div_id_dlicense" class="form-group required">
        <label for="id_dlicense" class="control-label col-md-3  requiredField"> Driving License<span class="asteriskField">*</span> </label>
        <div class="controls col-md-9">
            <select class="form-control gradient" id="id_dlicense">
                <option>Please Select</option>
                <option>032654897</option>
            </select>
        </div>
    </div>
        <button disabled="disabled" type="submit" class="vhb next action-button" id="btn1">Continue</button>
</fieldset>

I have tried this code for the input fields but still not working I suspect it's because of the wrong selector, and I havent added the code yet for the select field. 我已经尝试过将这些代码用于输入字段,但是仍然无法正常工作,我怀疑这是因为选择器错误,并且我还没有为选择字段添加代码。

<script type="text/javascript">
$(document).ready(function() {
    var $fields = $('#details :input');
    $fields.keyup(function() {
        var $emptyFields = $fields.filter(function() {
            return $.trim(this.value) === '';
        });
        if (!$emptyFields.length) {
            $('#btn1').prop('disabled', true);
        } else {
            $('#bt1').prop('disabled', false);
        }
    });
});

First step : make it disabled by default Second step: make a function that follows this pseudo code 第一步:默认禁用它。第二步:创建一个遵循此伪代码的函数

on input change : function
   if(input 1.val()=="" || input 2.val()=="" /* etc... */) then
    set disabled to true
  else
    remove disabled attribute
  end if
end function 

BTW, in JQuery disabled mustn't a prop but must be an attr . 顺便说一句,在JQuery中, disabled不能是prop而必须是attr In JQuery you can use $(/*selector*/).removeAttr(/*attribute*/) to achieve it. 在JQuery中,您可以使用$(/*selector*/).removeAttr(/*attribute*/)来实现。

 $('#a').on('keyup paste propertychange input', function() { if (this.value === "") $('#b').prop('disabled', true); else $('#b').removeAttr('disabled'); }); $('#b').on('keyup paste propertychange input', function() { if (this.value === "") $('#c').prop('disabled', true); else $('#c').removeAttr('disabled'); }); $('#c').on('keyup paste propertychange input', function() { if (this.value === "") $('#d').prop('disabled', true); else $('#d').removeAttr('disabled'); }); $('#d').on('keyup paste propertychange input', function() { if (this.value === "") $('#e').prop('disabled', true); else $('#e').removeAttr('disabled'); }); $('#e').on('keyup paste propertychange input', function() { if (this.value === "") $('#f').prop('disabled', true); else $('#f').removeAttr('disabled'); }); $('#f').on('keyup paste propertychange input', function() { if (this.value === "") $('#g').prop('disabled', true); else $('#g').removeAttr('disabled'); }); $('#g').on('keyup paste propertychange input', function() { if (this.value === "") $('#h').prop('disabled', true); else $('#h').removeAttr('disabled'); }); $('#h').on('keyup paste propertychange input', function() { if (this.value === "") $('#i').prop('disabled', true); else $('#i').removeAttr('disabled'); }); $('#i').on('keyup paste propertychange input', function() { if (this.value === "") $('#j').prop('disabled', true); else $('#j').removeAttr('disabled'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> <form id="ridefinancing"> <fieldset id="details"> <h3 class="fs-title">Your Details</h3> <div id="div_id_firstname" class="form-group required"> <label for="id_firstname" class="control-label col-md-3 requiredField"> Firstname<span class="asteriskField">*</span> </label> <div class="controls col-md-9"> <input id="a" class="form-control gradient" id="id_firstname" maxlength="30" name="firstname" style="margin-bottom: 10px" type="text" /> </div> </div> <div id="div_id_lastname" class="form-group required"> <label for="id_lastname" class="control-label col-md-3 requiredField"> Lastname<span class="asteriskField">*</span> </label> <div class="controls col-md-9 "> <input id="b" disabled="disabled" class="form-control gradient" id="id_lastname" maxlength="30" name="lastname" style="margin-bottom: 10px" type="text" /> </div> </div> <div id="div_id_email" class="form-group required"> <label for="id_email" class="control-label col-md-3 requiredField"> Email<span class="asteriskField">*</span> </label> <div class="controls col-md-9"> <input id="c" disabled="disabled" class="form-control gradient" id="id_email" maxlength="30" name="email" style="margin-bottom: 10px" type="text" /> </div> </div> <div id="div_id_phone" class="form-group required"> <label for="id_phone" class="control-label col-md-3 requiredField"> Phone<span class="asteriskField">*</span> </label> <div class="controls col-md-9 "> <input id="d" disabled="disabled" class="form-control gradient" id="id_phone" maxlength="30" name="phone" style="margin-bottom: 10px" type="text" /> </div> </div> <div id="div_id_birthday" class="form-group required"> <label for="id_birthday" class="control-label col-md-3 requiredField"> Birthday<span class="asteriskField">*</span> </label> <div class="controls col-md-9"> <div class="row"> <div class="col-xs-4"> <select id="e" disabled="disabled" class="form-control gradient" id="id_day"> <option>1</option> <option>2</option> <option>3</option> </select> </div> <div class="col-xs-4"> <select id="f" disabled="disabled" class="form-control gradient" id="id_month"> <option>Please Select</option> <option>1</option> <option>2</option> <option>3</option> </select> </div> <div class="col-xs-4"> <select id="g" disabled="disabled" class="form-control gradient" id="id_year"> <option>2014</option> <option>2015</option> <option>2016</option> </select> </div> </div> </div> </div> <div id="div_id_status" class="form-group required"> <label for="id_status" class="control-label col-md-3 requiredField"> Marital Status<span class="asteriskField">*</span> </label> <div class="controls col-md-9"> <select id="h" disabled="disabled" class="form-control gradient" id="id_status"> <option>Please Select</option> <option>Single</option> <option>Married</option> <option>Divorced</option> </select> </div> </div> <div id="div_id_dlicense" class="form-group required"> <label for="id_dlicense" class="control-label col-md-3 requiredField"> Driving License<span class="asteriskField">*</span> </label> <div class="controls col-md-9"> <select id="i" disabled="disabled" class="form-control gradient" id="id_dlicense"> <option>Please Select</option> <option>032654897</option> </select> </div> </div> <button id="j" disabled="disabled" type="submit" class="vhb next action-button" id="btn1">Continue</button> </fieldset> 

check the form and script if i have done correct way . 检查表格和脚本是否正确。 Hope it will work for you . 希望它对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 vuejs 的输入文本中添加禁用属性? - How to add disabled attribute in input text in vuejs? 如何仅在使用javascript / jquery的其他输入不为空的情况下才添加必填属性? - how to add required attribute only if another input is not empty with javascript/jquery? 使用jQuery单击后退按钮后,输入字段不会被禁用 - Input fields are not getting disabled after clicking on back button using jquery 侦听空白输入并向按钮添加“禁用”属性,直到注意到输入 - Listen for blank inputs and add a “disabled” attribute to a button until an input is noticed 如果输入不为空,则启用禁用按钮 - Enable Disabled Button if Input is not empty 尝试有条件地禁用轮播类型应用程序的多个输入字段 - 如果输入字段为空,则应禁用下一个按钮 - Trying to conditionally disable multiple input fields for a carousel type app - The next button should be disabled if input field is empty 清除输入字段时如何使按钮再次禁用 - how to keep button disabled again when clearing the input fields 输入空时反应如何将按钮更改回禁用 - React how to change button back to disabled when input empty 必填字段为空时禁用按钮 - button disabled while required fields are empty 如何在Ember.Select中为选项添加“已禁用”属性 - How to add “Disabled” attribute to options in Ember.Select
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM