如何在字段填满之前禁用提交按钮?

时间:2016-08-02 07:42:47

标签: javascript jquery

JS

我知道这是一个简单的问题,但我无法为我的表单获得答案。我想在表单保存之前禁用提交按钮。



 (function() {
    $('form > input').keyup(function() {
      alert("helloo");

        var empty = false;
        $('form > input').each(function() {
            if ($(this).val() == '') {
                empty = true;
            }
        });

        if (empty) {
            $('.save-form').attr('disabled', 'disabled');
        } else {
            $('.save-form').removeAttr('disabled');
        }
    });
}) ()




查看

这是我的表格,其中包含各种字段

- content_for :javascript_includes do
  = javascript_include_tag "dropdown.js", "business.js", "bootstrap-toggle.min.js"
.col-sm-12
  .main-login.main-center.customer_details
    = nested_form_for(@customer_detail) do |f|
      - all_views_side_error_messages!(@customer_detail)
      .form-alignment
        .fieldset
          .row
            .col-sm-12
            = f.label :customer_name,"Organisation/Customer Name", class: "col-sm-3 control-label text-right"
            = f.text_field :customer_name, autofocus: true, class: "col-sm-3"
            = f.label :residential_type,"Residential Type", class: "col-sm-3 control-label text-right"
            = f.check_box :residential_type, autofocus: true, "data-width" => "200", class: "toggle-two-resident select-resident col-sm-3"
        %br

        .fieldset
          .row
            .col-sm-12
            = f.label :customer_id,"Customer ID", class: "col-sm-3 control-label text-right"
            = f.text_field :customer_id, autofocus: true, class: "col-sm-3"
            = f.label :attention_to,"Attention To", class: "col-sm-3 control-label text-right"
            = f.text_field :attention_to, autofocus: true, class: "col-sm-3"
        %br

        .fieldset
          .row
            .col-sm-12
            = f.label :address,"Address", class: "col-sm-3 control-label text-right"
            = f.text_area :address, autofocus: true, class: "col-sm-3"
            = f.label :city,"City", class: "col-sm-3 control-label text-right"
            = f.text_field :city, autofocus: true, class: "col-sm-3"
        %br

        .fieldset
          .row
            .col-sm-12
            = f.label :pin_code,"Pin Code", class: "col-sm-3 control-label text-right"
            = f.text_field :pin_code, autofocus: true, class: "col-sm-3"
            %div{id: 'state_field'}
              = f.label :state,"State", class: "col-sm-3 control-label text-right"
              = f.select(:state, options_for_select(State.collect_state),{},{class: "selectpicker col-sm-3 column-width-change", prompt: "Select State", "data-live-search": "true"})
            %div.hide{id: 'country_field'}
              = f.label :country,"Country", class: "col-sm-3 control-label text-right"
              = f.select(:country, CountryList.collect_country_list,{}, {class: "selectpicker add_class_country dropdown_country col-sm-3 column-width-change", title: "select country", "data-live-search": "true"})

        %br

        .fieldset
          .row
            .col-sm-12
            = f.label :email,"Email", class: "col-sm-3 control-label text-right"
            = f.email_field :email, autofocus: true, class: "col-sm-3"
            = f.label :contact_no,"Contact No", class: "col-sm-3 control-label text-right"
            = f.text_field :contact_no, autofocus: true, class: "col-sm-3"
        %br

        .fieldset
          .row
            .col-sm-12
            = f.label :website,"Website", class: "col-sm-3 control-label text-right"
            = f.text_field :website, autofocus: true, class: "col-sm-3"
            %div.hide{id: 'currency_field'}
              = f.label :currency_type,"Currency Type", class: "col-sm-3 control-label text-right"
              = f.select(:currency_type, CurrencyType.collect_currency_type,{},{class: "selectpicker dropdown_business column-width-change", prompt: 'Select', "data-live-search": "true"})
            %div{id: 'change_resident'}
              = f.label :business_type,"Business Type", class: "col-sm-3 control-label text-right"
              = f.select(:business_type, BusinessType.collect_business_type,{},{class: "selectpicker dropdown_business column-width-change", prompt: 'Select', "data-live-search": "true"})
              %br
              %br

        .fieldset
          .row
            .col-sm-12
            = f.label :service_tax_reg_no,"Service tax Registration No", class: "col-sm-3 control-label text-right"
            = f.text_field :service_tax_reg_no, autofocus: true, class: "col-sm-3"
            = f.label :pan_no,"PAN No", class: "col-sm-3 control-label text-right"
            = f.text_field :pan_no, autofocus: true, class: "col-sm-3"
        %br

        .fieldset
          .row
            .col-sm-12
            = f.label :local_sales_tax_reg_no,"Local Sales Tax Registration No", class: "col-sm-3 control-label text-right"
            = f.text_field :local_sales_tax_reg_no, autofocus: true, class: "col-sm-3"
            = f.label :central_sales_tax_no,"Central Sales Tax Registration No", class: "col-sm-3 control-label text-right"
            = f.text_field :central_sales_tax_no, autofocus: true, class: "col-sm-3"
        %br

        = render partial: 'customer_details/goods_address', locals: {customer_detail: @customer_detail}
        %br

        .fieldset
          .row
            .col-sm-12
              = f.label :opening_bal,"Opening Balance if any?", class: "col-sm-3 control-label text-right"
              = f.text_field :opening_bal, autofocus: true, class: "col-sm-3"
              = f.label :business_center,"From which business center invoice is being raised?", class: "col-sm-3 control-label text-right business_center-alignment"
              = f.select(:business_center, BusinessType.collect_business_type,{},{class: "selectpicker dropdown_business column-width-change", prompt: 'Select', "data-live-search": "true"})
        %br


        .fieldset
          .row
            - can_download = params[:action] == 'edit' ? false : true
            .col-sm-3
              = f.submit "Save", class: "btn btn-primary save-form"
            .col-sm-3
              = f.submit "cancel", type: :reset, class: "btn btn-primary"
            .col-sm-3
              %div{id: 'download_form'}
                - if @customer_detail.save
                  = link_to "Download", download_csv_customer_detail_path(@customer_detail.id, format: "csv"), class: "btn btn-primary", disabled: can_download
            .col-sm-3
              = link_to("Print", "javascript:print()", class: "btn btn-primary")

有人可以帮我解决这个问题吗?。提前谢谢

3 个答案:

答案 0 :(得分:1)

您必须使用prop代替attr

if (empty) {
    $('.save-form').prop('disabled', true);
} else {
    $('.save-form').prop('disabled', false);
}

甚至更短,不需要if左右,只需直接使用empty

$('.save-form').prop('disabled', empty);

<强> Working example.

答案 1 :(得分:0)

尝试纯JS:

if (empty) {
   document.getElementsByClassName(".save-form")[0].setAttribute('disabled','disabled');
} else {
   document.getElementsByClassName(".save-form")[0].removeAttribute('disabled');
}

答案 2 :(得分:0)

的Javascript

NodeList.prototype.addEventListener = function(e, handler) {
    for(var i = 0;i<this.length;i++)
        this[i].addEventListener(e, handler);
};
var inputs = document.getElementsByClassName("text-field");
inputs.addEventListener("keydown", function() {
    var noneEmpty = true;
    for(var input of inputs)
        if(input.value === "") 
            noneEmpty = false;
    document.getElementsByClassName("submit")[0].disabled = noneEmpty;
});

JQuery的

$(".text-field").keydown(function() {
    var noneEmpty = true;
    $(".text-field").each(function() {
        if($(this.attr("value") === "") {
            noneEmpty = false;
            return false;
        }
    });
    $(".submit").attr("disabled", noneEmpty);
});

可能的替代品:

.attr()

.prop() .keydown()

.on("keydown", ...)