【问题标题】:Disable field on focus with jquery使用jquery禁用焦点字段
【发布时间】:2017-09-08 14:04:18
【问题描述】:

我尝试创建一个 jquery 代码,它执行以下操作:

如果我将 id 为“ac_inp”的字段设为焦点,则 id 为“ac_sel”的选择字段将被禁用。如果我选择 id 为“ac_sel”的选择字段,则 id 为“ac_inp”的字段将被禁用。但它不起作用。它有什么问题?

$(document).ready(function () {
    if ($("#ac_inp").is(':focus')) {
        $("#ac_inp").prop('disabled', false)
        $("#ac_sel").prop('disabled', true)
    }
    if ($("#ac_sel").is(':focus')) {
        $("#ac_inp").prop('disabled', true)
        $("#ac_sel").prop('disabled', false)
    }
});

【问题讨论】:

  • 附加到事件..这只会在加载时发生
  • $( "#target" ).focus(function() { //you code here }); 喜欢这个
  • 那是什么意思?你有我的例子吗?
  • 上面给出的条件语句放在your code part
  • 如果您可以添加 html 标记,我可以提供更多帮助

标签: jquery select input focus


【解决方案1】:

在您的 sn-p 中,您准备一个在文档加载时运行一次的脚本。

在这种情况下,您需要为焦点创建事件,而不是一个不再运行的检查。

如果你有这个基本的疑问,请考虑在开始编码之前学习入门级书籍,否则你会浪费很多时间,理解基本概念非常重要。

<script>
$(document).ready(function () {

  $("#ac_inp").focus(function() {
     $(this).prop('disabled', false);
     $("#ac_sel").prop('disabled', true);
  });

  $("#ac_sel").focus(function() {
    $(this).prop('disabled', false);
    $("#ac_inp").prop('disabled', true);
  });

});
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-26
    • 2018-09-23
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多