【问题标题】:What do I use to target this input to disable it with jquery .attr("disabled", "disabled")我用什么来定位这个输入以使用 jquery .attr("disabled", "disabled") 禁用它
【发布时间】:2016-08-31 03:12:26
【问题描述】:

我已经测试了代码,它适用于其他输入,但由于某种原因,此输入不起作用。输入使用rails-jquery-autocomplete gem,它使用jquery autocomplete

导轨代码:

<%= f.autocomplete_field :tag_list, autocomplete_tag_name_photos_path, :"data-delimiter" => ', ', 'data-auto-focus' => true, :id_element => '#tag_element' %>

源代码:

<input data-delimiter=", " data-auto-focus="true" data-autocomplete="/photos/autocomplete_tag_name" data-id-element="#tag_element" type="text" name="photo[tag_list]" id="photo_tag_list" class="ui-autocomplete-input" autocomplete="off">

脚本

$(document).ready(function(){
  $('.glyphicon-plus').on("click", function(){
    if($(window).width() > 768) {
      console.log
      $('???').attr("disabled", "disabled")
    }
  });
});

我尝试过:#photo_tag_list、#tag_element、#photo.tag_list.ui-autocomplete-input、input#photo_tag_list。

【问题讨论】:

  • 你需要重新检查你的代码,你这里有错误-> console.log.

标签: jquery jquery-ui autocomplete


【解决方案1】:

试试下面的代码。

$(document).ready(function(){
  $('.glyphicon-plus').on("click", function(){
    if($(window).width() > 768) {

      $('#photo_tag_list').attr('readonly', true);// IF JQUERY 1.9+   $('#inputId').prop('readonly', true);
    }
  });
});

原因:输入类型文本不支持禁用,要使输入类型文本禁用,我们需要使用只读属性。

【讨论】:

  • 这行得通,我会接受它作为答案,虽然我猜输入仍然在 DOM 中?我正在尝试将它与这个主题一起使用 - stackoverflow.com/questions/39234293/…
  • 你想从 DOM 中删除元素吗?
  • 如果您在评论中看到其他主题...我想要两个输入,然后根据 $(window).width 在它们之间切换...是的,我希望能够根据 $(window).width 切换/删除输入
【解决方案2】:

HTML 元素将ui-autocomplete-input 作为一个类。我认为js代码是错误的,因为监听器是为glyphicon-plus类附加的。

希望能帮到你!

【讨论】:

  • glyphicon被点击,然后运行中间的代码。
猜你喜欢
  • 2011-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
相关资源
最近更新 更多