Javascript 即使在搜索完成后,我也希望保留输入字段数据

Javascript 即使在搜索完成后,我也希望保留输入字段数据,javascript,java,spring,Javascript,Java,Spring,上面是我的控制器代码 @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.p

上面是我的控制器代码

@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";
}

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

编辑后记录

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


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

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

<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)

在jsp中,而不是value=“键入要搜索的员工姓名…” 应使用placeholder=“键入要搜索的员工姓名…”,这样即使搜索完成,它也能正常工作并保留输入字段数据。 谢谢你的建议……


$(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');
            }
        });});

那么,ViewPage是否与您用于搜索的JSP相同?或者它包含搜索片段?在您发布的片段中没有表单开始标记。是否在表单开始标记上设置了modelAttribute=“employee”属性?有了这些信息,就不需要在模型中设置employeeName,因为输入将从员工模型中获取其值。是的,我在jsp页面中设置了我的modelAttribute=“employee”所有值都没有进入页面或部分进入页面?一切正常,但输入文本框在进入搜索页面后将重置为默认值,即,键入要搜索的员工的姓名。。。在搜索页面中,您默认设置的“值”会产生问题。没错,但是我应该在哪里修复代码中的错误?请检查答案。
 <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'}" />
$(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');
            }
        });});
<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...'"/>