如何在jQuery中启用和禁用文本框 [英] How to make text box enable and disable in jquery

查看:109
本文介绍了如何在jQuery中启用和禁用文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了html和脚本的示例代码,如下所示: 当我首先执行此代码时,我会收到警报提示,但是当我通过按Tab键在cca处更改时会收到其他警报,然后它不会显示警报.如何使用该文本框以及启用和禁用它的其他文本字段.

I have written a sample code of html and script as follows: When i execute this code first i will get that alert hello but other alert when i change at cca by pressing tab button then it is not showing alert. How to use that text box and enable and disable other text fields of it.

HTML:

<div id="cca" class="leaf">
     <label class="control input text" title="">
        <span class="wrap">cca</span>
        <input class="" type="text" value="[Null]">
        <span class="warning"></span>
     </label>
</div>

JS:

jQuery(document).ready(function () {        
    alert("hello");        
    jQuery("#cca label.control input").on('change', function (event) {
        alert('I am pretty sure the text box changed');
        event.preventDefault();
    });
});

推荐答案

我不确定您要做什么,但是我可以帮助您使警报正常运行.您基本上没有正确使用jQuery的"on"功能.

I'm not really sure what you are trying to do but I can help get the alert working. You are basically not using jQuery "on" function correctly.

$('#thisNeedsToBeContainer').on('focusout', '#elemToBindEventTo', function (event)....

以下其中一项将满足您的需求:

One of the following will do what you need:

在离开文本框时将触发

$(document).ready(function () {      

    alert("hello");        
    $("#cca").on('focusout', 'label.control input', function (event) {
        alert('I am pretty sure the text box changed');
        event.preventDefault();
    });
});

这将在change

$(document).ready(function () {       
    alert("hello");        
    $("#cca").on('change', 'label.control input', function (event) {
        alert('I am pretty sure the text box changed');
        event.preventDefault();
    });
});

这会在键入时在keyup上触发

$(document).ready(function () {  
    alert("hello");        
    $("#cca").on('onkeyup', 'label.control input', function (event) {
        alert('I am pretty sure the text box changed');
        event.preventDefault();
    });
});

请参见 JsFiddle

您还应该关闭输入:

<input class="" type="text" value="[Null]"/>

这篇关于如何在jQuery中启用和禁用文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆