【问题标题】:Execute function when a user has entered more than 5 characters in text box当用户在文本框中输入超过 5 个字符时执行函数
【发布时间】:2015-01-23 11:47:42
【问题描述】:

当用户在文本框中输入超过 5 个字符时,我正在尝试运行 jQuery 函数。到目前为止,我已经设法让我的脚本使用 keyup 函数运行,但这会导致脚本在用户输入第一个字符的那一刻运行,我只希望它在用户输入 5 或更多时运行字符。

这是我的代码,我是 jquery 的新手,所以如果有人能告诉我我哪里出了问题,我将不胜感激。提前致谢,

<script type="text/javascript">
    $(function() {
        //alert('Document is ready');
        $('#search').keyup(function() {
            if ($("#search").val().length > 3)
                var sel_stud = $(this).val();
                //alert('You picked: ' + sel_stud);

                $.ajax({
                    type: "POST",
                    url: "include/fetch_search.php",
                    data: 'theOption=' + sel_stud,
                    success: function(whatigot) {
                        //alert('Server-side response: ' + whatigot);
                        $('#search_results').html(whatigot);
                        $('#theButton').click(function() {
                        alert('You clicked the button');
                    });
                } //END success fn
            }); //END $.ajax
        })}; //END dropdown change event
    }); //END document.ready
</script>

【问题讨论】:

  • 你忘记了括号并放置了 3 而不是 5 ...我认为问题在于
  • 要添加到已经提到的解决方案,如果您的用户不断输入您的 ajax 调用将触发很多次。您应该考虑到这一点,并且每个时间间隔(或按下的键)只进行一次 ajax 调用。

标签: jquery


【解决方案1】:

试试这个。您忘记将键放在 if 语句上。

<script type="text/javascript">
            $(function() {
//alert('Document is ready');

                $('#search').keyup(function ()  {
                    if($("#search").val().length > 5) {

                        var sel_stud = $(this).val();

                        $.ajax({
                            type: "POST",
                            url: "include/fetch_search.php",
                            data: 'theOption=' + sel_stud,
                            success: function(whatigot) {
    //alert('Server-side response: ' + whatigot);
                                $('#search_results').html(whatigot);
                                $('#theButton').click(function() {
                                    alert('You clicked the button');
                                });
                            } //END success fn
                        }); //END $.ajax
                    }
                })}; //END dropdown change event
            }); //END document.ready
        </script>

这就是你所拥有的:

if($("#search").val().length > 3)

if 语句不会做任何事情,你必须在其中包装一些代码,这就是上面的代码所做的(并将 3 更改为 5 :S)。

【讨论】:

  • 感谢您的回复,但是我试过了,脚本仍然没有执行:(
  • 哦,好的,设法让它工作,我不得不从结束下拉更改事件中删除其中一个括号。一旦它允许我就会接受。谢谢
【解决方案2】:

您需要签到 5 而不是 3

        if ($("#search").val().length >=5)

还有这一行

       })}; //END dropdown change event

应该修改成这个

       }); //you just added an extra } at the end

我想休息很好

【讨论】:

    【解决方案3】:
            $(function() {
                $('#search').keyup(function (e)  {    
                if($(this).val().length >= 5)
                    alert('5th character entered');
                }) 
            }); //END document.ready
    

    同时从代码中删除多余的结尾花括号:

     }); //END dropdown change event
    

    而不是

     })}; //END dropdown change event
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-06
      • 1970-01-01
      • 2013-11-15
      相关资源
      最近更新 更多