gpt4 book ai didi

javascript - 在填写字段之前如何禁用提交按钮?

转载 作者:行者123 更新时间:2023-11-29 21:20:12 26 4
gpt4 key购买 nike

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")

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

最佳答案

你必须使用 prop 而不是 attr:

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

或者更短,不需要if,直接使用empty:

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

Working example.

关于javascript - 在填写字段之前如何禁用提交按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38714541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com