1

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

<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,并且根据该响应将显示在列表中。

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

4

2 回答 2

5

试着举个例子:

$('#searchField').keyup(function() {
  var val = $.trim( this.value );
  if(val.length == 3) {
    // do something
     .....
     ....
    var finalStr = "system"+encodeURIComponent("|^")+"0"+encodeURIComponent("|^")+ val;
  }
})
于 2012-06-25T06:29:55.570 回答
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 示例

于 2012-06-25T06:53:01.060 回答