jquery禁用不工作
我正在开发一个Rails应用程序。在index.html.haml上,我还呈现了_form.html.haml(用于新建和编辑操作)。在索引页面上有一个“添加组”链接,单击该链接时应禁用表单中的某些字段。我已经为它编写了jquery代码。我的问题是,当我单击链接时,字段被禁用(灰显),但会立即重新启用。如果刷新页面,则会看到禁用的字段。我希望它发生时,我点击链接,然后保持禁用 jQuery代码jquery禁用不工作,jquery,ruby-on-rails,Jquery,Ruby On Rails,我正在开发一个Rails应用程序。在index.html.haml上,我还呈现了_form.html.haml(用于新建和编辑操作)。在索引页面上有一个“添加组”链接,单击该链接时应禁用表单中的某些字段。我已经为它编写了jquery代码。我的问题是,当我单击链接时,字段被禁用(灰显),但会立即重新启用。如果刷新页面,则会看到禁用的字段。我希望它发生时,我点击链接,然后保持禁用 jQuery代码 $(function(){ $("#add_requirement_group").click
$(function(){
$("#add_requirement_group").click(function() {
$("#requirement_text_full, #requirement_requirement_type").prop("disabled", true);
});
});
用于新建和编辑操作的表单。
= form_for ([@requirement.requirements_template, @requirement]), html: { class: "form-horizontal" } do |f|
- if @requirement.errors.any?
#error_explanation
%h2= "#{pluralize(@requirement.errors.count, "error")} prohibited this requirement from being saved:"
%ul
- @requirement.errors.full_messages.each do |msg|
%li= msg %fieldset
.control-group
= f.hidden_field :parent_id
.control-group
= f.label :text_brief, class: "control-label"
.controls
= f.text_field(:text_brief, class: "input-block-level")
.control-group
= f.label :text_full, class: "control-label"
.controls
= f.text_area(:text_full, rows: 5, class: "input-block-level")
.control-group
= f.label :obligation, class: "control-label"
.control-group
= f.label :requirement_type, class: "control-label"
.controls
= f.select :requirement_type, options_for_select([:text, :numeric, :date, :enum]), include_blank: true
.actions.pull-right
%br/
= f.submit 'Save', class: 'btn btn-info'
= button_to 'Finish', '#', class: 'btn btn-info', method: :get
index.html.haml
%hr
.row-fluid
.span4
%fieldset.template_outline
%strong Template Outline
%br
= link_to 'Add Group', new_requirements_template_requirement_path(@requirements_template), id: "add_requirement_group"
%hr
= render 'form'
我知道这是一段非常简单的代码,但由于某些原因,禁用功能没有按预期工作。好的。单击“添加组”链接时,将触发click事件处理程序,这将禁用HTML控件,正如您所期望的那样。但是,它将继续并重定向到“new_requirements_tempate_requirement_path”,我认为您不希望它这样做。为了抑制DOM元素的默认操作,您可能需要使用“preventDefault()
您描述的行为告诉我您使用的是Turbo链接,它是Rails 4上的标准配置。您好,它成功了!非常感谢您。不过,有一个简单的问题。您说“为了抑制DOM元素的默认操作”,这是什么意思“在这种情况下?别想明白你的意思。再次感谢你,没问题。如果你能把我的答案记下来,我将不胜感激。谢谢。:)
$(function(){
$("#add_requirement_group").click(function(e) {
e.preventDefault();
$("#requirement_text_full, #requirement_requirement_type").prop("disabled", true);
});
});