【问题标题】:How to properly disable spans used with input-group-addon如何正确禁用与 input-group-addon 一起使用的跨度
【发布时间】:2015-03-10 18:44:18
【问题描述】:

我正在使用 Bootstrap,并且我与 input-group-addon 类有一个跨度。我们都知道,将附加组件与其他组件一起使用是相当普遍的。我遇到的问题是在禁用与该类相关的输入后禁用具有此类的跨度。

在我的示例中,我使用带有引导日期选择器组件的日历插件。

<div class="input-group date">

    <input type="text" class="form-control" id="dp1" />

    <span class="input-group-addon" id="dp1Icon"><img src="<%=context%>/images/calendar-glyph.png"></span>

</div>

我这样定义我的 Bootstrap 日期选择器:

var datePicker = $(".input-group.date").datepicker({myOptions});

有时我会像这样禁用我的日期选择器:

$("#dp1").prop("disabled", true);

但我无法禁用 dp1Icon。我研究了不少。首先,引导日期选择器中没有“显示”方法可以让我捕获和防止默认值。

其次,“pointer-events:none”在 IE 上不起作用。

第三,使用这个也不行:

$("#dp1Icon").click(function(e){// if it has a certain class, e.preventDefault()});

也不会工作。

我可以像这样轻松删除点击处理程序:

$("#dp1Icon").off("click");

但是我如何重新分配它以再次打开日期选择器?这不起作用:

$("#dp1Icon").on("click",$(".input-group.date").datepicker("show");

我还有什么解决方案?感谢您提供任何有用的提示。

【问题讨论】:

  • 暂时用 css 隐藏 span 并替换为具有灰色图标的相同元素?

标签: javascript jquery html twitter-bootstrap


【解决方案1】:

概念证明:

$("#dp1Icon_blocked").off(); //remove all event 
function disable()
{
$("#dp1").prop("disabled", true);
$("#dp1Icon").addClass("hidden");
$("#dp1Icon_blocked").removeClass("hidden")

}

function enable()
{
$("#dp1").prop("disabled", false);
$("#dp1Icon").removeClass("hidden");
$("#dp1Icon_blocked").addClass("hidden")

}

//display purposes not part of the solution:
$($("button")[0]).click(disable);
$($("button")[1]).click(enable);
.hidden{
 display: block;
}

.blocked {
   color: #c2c2c2 !important; /* The important overrides the settings from bootstrap */
   cursor: not-allowed;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>

<div class="input-group date">

    <input type="text" class="form-control" id="dp1" />

    <span class="input-group-addon" id="dp1Icon">Date</span>
    <span class="input-group-addon blocked hidden" id="dp1Icon_blocked">Date</span>
</div>

<button>Disable</button><button>Enable</button>

【讨论】:

  • 不工作。不太确定你在这里做什么。您不会通过添加隐藏类来隐藏 dp1Icon 。不允许的光标不会阻止日期选择器在单击时显示。
  • 试图隐藏原始元素 #dp1Icon 并将其替换为相同的元素。认为它可能会起作用。
  • 对,这完全有道理,但您仍然没有阻止点击 dp1Icon_blocked。您所做的只是将光标设置为不允许...这是行不通的。
  • @fumeng 使用 jQuery 的 off() 替换元素(保留原始元素)作为此处的编辑。这可能行得通。
  • .off() 绝对可以删除点击事件。但是我如何重新分配它呢?我更新了我的原始帖子,向您展示我的尝试。但它不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 2013-08-07
  • 2017-11-09
  • 2017-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多