【问题标题】:i want to retain my input field data even after search is done即使在搜索完成后,我也想保留我的输入字段数据
【发布时间】:2016-01-19 07:06: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 页面代码。 我的问题是,当我从下拉列表中选择某个选项并在搜索后根据下拉值输入文本条目搜索时,我的输入字段转到默认文本,但我想将其保留在搜索结果页面中。

【问题讨论】:

  • 所有值都没有进入页面或部分?
  • 一切正常,但进入搜索页面后输入文本框将重置为默认值,即在搜索页面中键入要搜索的员工姓名...
  • 您的“值”默认设置会导致问题。
  • 没错,但我应该在哪里修复代码中的错误
  • 请检查答案。

标签: javascript java spring


【解决方案1】:

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

@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 页面上,你可以获得像 as 一样的值

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

【讨论】:

    【解决方案2】:

    后期编辑记录

    解决方案只是将搜索输入字段中的 value="" 转换为 placeholder=""。

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

    【讨论】:

    • 那么,ViewPage 是否与您用于搜索的 JSP 相同?还是包含搜索片段?
    • 您发布的 sn-p 中没有表单开始标签。您是否在表单开始标签上设置了 modelAttribute="employee" attr?有了这些信息,就不需要在模型中设置employeeName,因为输入将从员工模型中获取其值。
    • 是的,我在我的 jsp 页面中设置了 modelAttribute = "employee"
    【解决方案3】:

    请修改代码如下:(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');
                }
            });});
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:
        <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...'"/>
        

        【讨论】:

          猜你喜欢
          • 2018-08-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-20
          • 2019-01-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多