我想在搜索完成后保留我的输入字段数据

i want to retain my input field data even after search is done

本文关键字:我的 保留 输入 字段 数据 搜索      更新时间:2023-09-26
@RequestMapping(value= "/search")
public String personList(@ModelAttribute("employee") Employee e,ModelMap map,@RequestParam("drop") String value) 
{
    /*System.out.println("In Controller " + e.getEmployeeName() +" "+ value);*/
    map.put("drop",value);
    map.put("employee",e);
    map.put("employeeList", employeeService.employeeList(e.getEmployeeName(),value)); 
    return "ViewPage";
}

以上是我的控制器代码

<form:form  action="search.html" modelAttribute="employee" method="POST">
<select id="ViewByName" class ="dropdown" name= "drop">
 <!-- <option value="r">Select....</option>  -->
  <option value="s">Starts With</option>
  <option value="c">Consist Of</option>
  <option value="e">Ends With</option>
</select>
            <div id="search-inner">
                <td>
                <form:input path="employeeName" type="text" name="search" maxlength="30" id="searchfield" title="searchfield" value="type the name of employee to search..." class = "search" 
                onfocus="if (this.value=='type the name of employee to search...') this.value=''" 
                 onblur="if (this.value == '') this.value ='type the name of employee to search...'"/>
              <input type="submit" name="Search" value="Name Search"   title="Search" />
                </td>
            </div>
</form:form>

以上是我的JSP页面代码。我的问题是当我从下拉列表中选择某个选项并在搜索后根据下拉值输入文本输入搜索时,我输入字段转到默认文本,但我想将其保留在搜索结果页面中。

您可以使用请求参数,如下所示:

@RequestMapping(value= "/search")
public String personList(@ModelAttribute("employee") Employee e,ModelMap     map,@RequestParam("drop") String value,HttpServetRequest request) 
{
    request.setparameter("searchdata",drop);
    /*System.out.println("In Controller " + e.getEmployeeName() +" "+ value);*/
    map.put("drop",value);
    map.put("employee",e);
       map.put("employeeList",employeeService.employeeList(e.getEmployeeName(),value)); 
    return "ViewPage";
}

在 jsp 页面上,您可以获得如下值

<%= request.getParameter("searchdata") %>

记录后编辑

解决方案只是将搜索输入字段上的值="转换为占位符="。

值覆盖了来自绑定的信息。

请按如下方式更改代码:(jsp)

 <form:input path="employeeName" type="text" name="search" maxlength="30" id="searchfield" title="searchfield" class = "search"  value="${not empty employee.employeeName? employee.employeeName : 'type the name of employee to search'}" />

并添加 jQuery:

$(document).ready(function() {
  $("#searchfield").focus(function(){
    if (this.value == "type the name of employee to search")
    {
        this.value = "";
        $(this).css('color','#000000');
     }
    });
    $("#searchfield").blur(function(){
            if (this.value == "")
            {
                this.value = "type the name of employee to search";
                $(this).css('color','#a9a9a9');
            }
        });});

在 jsp 中而不是 value="键入要搜索的员工的姓名..."占位符="键入要搜索的员工的姓名..."应该使用,然后它工作正常,即使在搜索完成后也会保留输入字段数据。感谢您的所有建议。

<form:input path="employeeName" type="text" name="search" maxlength="30" id="searchfield" title="searchfield" placeholder="type the name of employee to search..." class = "search" 
            onfocus="if (this.value=='type the name of employee to search...') this.value=''" 
             onblur="if (this.value == '') this.value ='type the name of employee to search...'"/>