【问题标题】:Any listener for input type text输入类型文本的任何侦听器
【发布时间】:2012-06-25 06:22:36
【问题描述】:

嗨,在我的输入类型文本中,如果用户输入假设三个字符,我想调用 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,并根据该响应将显示在列表中。

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

【问题讨论】:

  • 使用keyup事件并检查输入文本的长度
  • 可以通过返回 json 使响应更简单

标签: jquery html jquery-ui jquery-mobile


【解决方案1】:

试着举个例子:

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

【讨论】:

    【解决方案2】:

    您可以使用.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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 2014-01-20
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      相关资源
      最近更新 更多