javascript 使用 JQuery 验证文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5241957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:30:13  来源:igfitidea点击:

Validate a textbox using JQuery

javascriptjquery

提问by user646751

I have TextBox and a Button

我有 TextBox 和一个按钮

first the button is in disabled position When the user starts typing the text in textbox the button should be enabled

首先按钮处于禁用位置当用户开始在文本框中键入文本时,应启用按钮

How can i achieve this using JQuery or Java Script

我如何使用 JQuery 或 Java Script 实现这一点

采纳答案by Bhanu Prakash Pandey

you can try with this also

你也可以试试这个

 <input type='text' id='textbox'>
    <input type="button" class="button-disabled" id="change" disabled="disabled" value="click">


    $("#textbox").keyup(checkForm).focus(checkForm);
    function checkForm()
    {
        if($("#textbox").val()=='')
           {
          $("#change").addClass("button-disabled").removeClass("button");
          $("#change").attr("disabled","disabled");
           }
           else
            {
           $("#change").removeClass("button-disabled").addClass("button");
           $("#change").removeAttr("disabled");
            }
       }

回答by Pradeep

Seems like you are a newbie to jQuery. I would say you start with jQuery Tutorialsand then move on to jQuery Validate. If you prefer books to start with, you can pick up a copy of jQuery in Action.

看起来您是 jQuery 的新手。我会说你从jQuery Tutorials开始,然后转向jQuery Validate。如果您更喜欢从书籍开始,您可以选择jQuery in Action的副本。

回答by DidierDotNet

<input type='text' id='textbox'>
<input type="button" id="button'" value="click me"> 

$('#button').attr('disabled', 'disabled');

$('#textbox').change(function(){$('#button').removeAttr('disabled')} );

回答by Praveen Prasad

<input type="text" id="myText">
<input type="submit" id="myButton" disabled="disable"/>


jQuery(function(){

jQuery('#myText').bind('keypress',function(e){
    if((jQuery(e.target).val()+"").length>0)
    {
       jQuery('#myButton').removeAttr('disabled');
    }
    else
    {
       jQuery('#myButton').attr('disabled','disable');
    }
});

});

回答by Dolan Antenucci

HTML:

HTML:

<input type='text' id='textbox'>
<input type="button" id="mybutton" value="click me">

JS:

JS:

$(document).load(function() {
    $('#mybutton').attr('disabled', 'disabled');
}

$('#textbox').change(function() {
    $('#mybutton').removeAttr('disabled');
}

Update: regarding the use of jQuery w/ ASP.NET, keep in mind that ASP.NET outputs standard HTML once the page is rendered, so the above code would work similarly, except you need to figure out the ID's of the textboxes generated by ASP.net. See this link for further explanation on this:
http://www.search-this.com/2009/08/06/using-jquery-with-asp-net-controls/

更新:关于 jQuery w/ASP.NET 的使用,请记住,一旦页面呈现,ASP.NET 会输出标准 HTML,因此上述代码的工作方式类似,除非您需要找出由生成的文本框的 ID ASP.NET。有关此问题的进一步说明,请参阅此链接:http:
//www.search-this.com/2009/08/06/using-jquery-with-asp-net-controls/

回答by JohnP

<input type='text' id='thetext' value=''>
<input type='button' disabled='disabled' id='thebutton' value='the button'>

$(document).ready(function(){
   $('#thetext').change(function(){
      $('#thebutton').removeAttr('disabled');
   });
});

Read up on the jQuery API : http://api.jquery.com/

阅读 jQuery API:http: //api.jquery.com/