输入类型文本的任何侦听器

时间:2012-06-25 06:22:36

标签: jquery html jquery-ui jquery-mobile

您好我的输入类型文本我想调用URL如果用户输入假设三个字符怎么做 如何添加监听器?我正在开发搜索应用程序现在我在下面的代码中发送硬编码值“oil”。这是我的代码:

<body>

<div data-role="page" id="mainPage">

    <div data-role="header">
        <h1>jQM Autocomplete</h1>

    </div>

    <div data-role="content">


        <p>
            <input type="text" id="searchField" placeholder="Categories">
            <ul id="suggestions" data-role="listview" data-inset="true"></ul>
        </p>

        <p>
            <a href="https://github.com/commadelimited/autoComplete.js" data-role="button">Download the code</a>
        </p>


    </div>

</div>

<script>

    $("#mainPage").bind("pageshow", function(e) {

        var substr,out,pra;

            var finalStr = "system"+encodeURIComponent("|^")+"0"+encodeURIComponent("|^")+"oil";
             var encodedURL = encodeURI("http://myDomainSearch.asp?requestString=");
             var parameters =  decodeURIComponent(finalStr);


        $.ajax({
        type: "POST",
        contentType:"application/x-www-form-urlencoded; charset=UTF-8",
        url: encodedURL,
        data: parameters
      }).done(function( msg )
              {
                  response  = msg
                    alert(response);
                    if(response.charAt(0)=='0')
                    {   
                        console.log("***"+ response);                                                         
                        substr = response.split('$');   

                        alert("inside if"+substr);

                        for (var out = 0;out<substr.length-1;out++)
                        {                       
                            pra = substr[out].split('|^');
                            console.log(">>>>>>>>>>>"+pra[0]);
                            console.log(">>>>>>>>>>>"+pra[1]);
                            console.log(">>>>>>>>>>>"+pra[2]);
                            console.log(">>>>>>>>>>>"+pra[3]);                  


                        }
                    }
                    else
                    {
                        alert("error")
                    }

                    $("#searchField").autocomplete({
                    target: $('#suggestions'),
                    source: pra,
                    link: 'target.html?term=',
                    minLength:1
                });
             }              
        );
    });
</script>

现在我想要的不是硬编码值,如果用户在上面的输入类型文本中输入三个字符,那么它会点击URL并根据该响应显示在列表中。

任何帮助将不胜感激。提前谢谢。

2 个答案:

答案 0 :(得分:5)

试着举个例子:

$('#searchField').keyup(function() {
  var val = $.trim( this.value );
  if(val.length == 3) {
    // do something
     .....
     ....
    var finalStr = "system"+encodeURIComponent("|^")+"0"+encodeURIComponent("|^")+ val;
  }
})

答案 1 :(得分:0)

您可以使用.live收听全球按键事件

<form>
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" id="txtHidden" style="display:none"/>
</form>​​​​​​​​​​

jQuery的:

$(document).live('keypress', function (e) {

$('#txtHidden').val($('#txtHidden').val() + String.fromCharCode(e.which));

    if($('#txtHidden').val() == "oil")
        alert("Black gold!");
});​

jsfiddle example