匹配特定值时刷新文本框

refresh text box when a particular value is matched

本文关键字:刷新 文本      更新时间:2023-09-26

我在index.jsp中有一个文本框,在写了一些东西之后,我用jquery调用了一个jsp。但我的要求是,如果一个特定的值将在服务器端匹配,那么我如何自动刷新该文本框?这意味着假设我输入了apple,然后通过onkeyup事件,我将名称(apple)发送到check.jsp,在这个jsp页面中,我将apple保存在一个字符串中。因此,现在index.jsp中会出现一个警告,即apple已经存在,并且该文本框将自动刷新。我该怎么做?我是这方面的初学者。

index.jsp

<script type="text/javascript">
  $(document).ready(function() {
    $("#textbox").keyup(function () {
        $.getJSON('check.jsp', {
            textboxname: this.value
        });
    });
  }); 
</script>

体内

<input type="text" id="textbox" name="textboxname"/>

check.jsp

String textboxname=request.getParameter("textboxname");

在keyup上将值发送到服务器,如果匹配,则返回true或false,然后在成功处理程序中检查如果其true刷新而不执行,则返回什么

$("#textbox").keyup(function () {
        $.getJSON('check.jsp', {
            textboxname: this.value
        },function(data){
          if(data.isTrue){
              alert("the value already exists");
              $("#textbox").val(''); //clear the text box                                  
         }
          else
              //do something                 
         });
    });
}); 

在服务器上不是确切的servlet代码,但您会得到的想法

JSONObject match = new JSONObject();
 //if value matches 
     match.put("isTrue","true");
 else
    match.put("isTrue","false");
response.setContentType("application/json");
response.getWriter().write(match.toString());true