单击禁用的文本框时显示警告消息

时间:2014-05-13 12:34:20

标签: javascript jquery yii

我想检查文本框是否已禁用。如果我们尝试单击禁用的文本框。它应该显示一条警告信息。

这是我的型号代码:

var isDisabled = $("#Travellerpaymentinfo_cc_number").prop('disabled');
if(isDisabled==1)
{
    $("#Travellerpaymentinfo_cc_number").click(function(){
      alert("The textbox is clicked.");
    });
}

4 个答案:

答案 0 :(得分:4)

由于禁用的控制元素不能绑定到它们的事件,因此您需要将事件绑定到祖先元素并检查目标是否为输入。

然后您可以继续检查是否已禁用,如果是,请显示提醒:

$(document).on('click',function(e){
    if(e.target.id == "Travellerpaymentinfo_cc_number" && e.target.disabled){
        alert("The textbox is clicked.");
    }
});

JSFiddle

答案 1 :(得分:2)

事件不适用于已禁用的元素,您可以将其作为解决方法

执行此操作

<强> HTML

添加readOnly属性

<textarea id="Travellerpaymentinfo_cc_number" class="disabled" readonly="true"></textarea>

<强> CSS

使用css

禁用它
.disabled{
    background:lightgrey;    
}

<强>的jQuery

您可以将if语句放在click事件中,如下所示

$("#Travellerpaymentinfo_cc_number").click(function () {
    if (this.readOnly) {
        alert("The textbox is clicked.");
    }
});

DEMO

答案 2 :(得分:0)

如果禁用了您的控制,则也可以禁用您可以触发的事件。您可以做的一件事是,您将文本框设置为只读,这样您就可以在有人点击它时触发事件

<input type="text" id="textbox" readonly="true" />

$(document).ready(function(){
    $('#textbox').click(function(){
        alert("not allowed");
    });
});

答案 3 :(得分:-3)

$(document).click(function (e) {
  if ($("#"+e.target.id).prop('disabled') && e.target.id=="Travellerpaymentinfo_cc_number"){
    alert("The textbox is clicked. ");
  }
});

isDisabled只是一个布尔值(真或假)

相关问题