0

我需要将输入字段和选择器合二为一。

我有 :

<form:select> 
<form:options items="${data}">
<form:select/>

而且我还有:

<form:input>

因此,当用户尝试从中选择某些<form:select>内容但没有找到时,他会将其自己放到以下标签<form:input>中。

如何使用 Spring 表单标签的 JQuery 或 JSP-JSTL 实现?这可能吗?

我的jsp:

html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<title>Страница выборки</title>
<link rel="stylesheet" href="resources/cssFiles/jquery-ui.css"/>
<script type="text/javascript" src="resources/jsFiles/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="resources/jsFiles/jquery-ui.js"></script>
<script type="text/javascript" src="resources/jsFiles/jquery-ui-i18n.js"></script>
<script type="text/javascript">

 $(document).ready(function()
         {

         $("#parctdate, #chldAdmitDate, #chldSchlDate, #name, #type, #daySchdl, #workSchdl, #rotation, #numbch, #chUnder3, #chUpper3, #chGoSchool, #chAdmitted").mouseenter(function() {        
             $(this).css("background-color", "gainsboro");   
         });

         $("#parctdate, #chldAdmitDate, #chldSchlDate, #name, #type, #daySchdl, #workSchdl, #rotation, #numbch, #chUnder3, #chUpper3, #chGoSchool, #chAdmitted").mouseleave(function() {        
             $(this).css("background-color", "white");   
         });

         var enabledDays = ["6-1-2013", "7-1-2013", "8-1-2013", "9-1-2013", "10-1-2013", "11-1-2013"];
         function nationalDays(date) {
                var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();            
                for (i = 0; i < enabledDays.length; i++) {
                    if($.inArray((m+1) + '-' + d + '-' + y,enabledDays) != -1 || new Date() > date) {           
                        return [true];
                    }
                }
                return [false];
            }

         $(function(){
               $.datepicker.setDefaults($.extend($.datepicker.regional["ru"]));
               $("#datepicker1, #datepicker2, #datepicker3").datepicker({dateFormat: "yy-mm-dd",
                                                                         duration: "normal",
                                                                         numberOfMonths: [ 1, 2 ],
                                                                         constrainInput: true,
                                                                         beforeShowDay: nationalDays});   
             });         

         $('#myform').submit(function(e) {

                if ($('#myselect').val()) {
                    // select is used, get the value
                    $('#institution.nameOfInstitution').val($('#myselect').val());

                } else if ($('#myinput').val()) {
                    // select was NOT used and textbox was used. try to get the value from textbox
                    $('#institution.nameOfInstitution').val($('#myinput').val());
                }
            }); 

     });

</script>

</head>

<body>

<spring:message code="label.input.button" var="labelbutton"/>

<h3 align="center"><spring:message code="label.input.topLabel"/></h3>


<form:form  id="myform" modelAttribute="fboAttribute" method="post" action="add" >
<table align="center">  

<tr id="name">
<td><form:label path="institution.nameOfInstitution"><spring:message code="label.input.nameOfInstitution"/></form:label>
<form:input id="myinput" path="institution.nameOfInstitution"/>
<form:select  id="myselect" path="institution.nameOfInstitution"> 
<form:option  value=""><spring:message code="label.select.msg" />-</form:option>
<form:options items="${listOfInstitutionsNames}"/>
</form:select> 
</td> <td><b><font color="#FF0000"><form:errors path="institution.nameOfInstitution"/></font></b></td>
</tr>

<tr>
<td><br></td>
</tr>

<tr>
<td><input type="submit" value="${labelbutton}"/></td>

</table> 
</form:form>

</body>

</html>
4

1 回答 1

1

我会使用隐藏输入来保留实际值。监听表单上的提交事件(通过 jQuery):

// assuming select value will be 0 when not selected..
$('#form_id').submit(function(e) {

    if ($('#select_id').val()) {
        // select is used, get the value
        $('#input_hidden_id').val($('#select_id').val());

    } else if ($('#input_id').val()) {
        // select was NOT used and textbox was used. try to get the value from textbox
        $('#input_hidden_id').val($('#input_id').val());
    }
});

并且#input_hidden_id将具有您的服务器需要的名称属性。

于 2013-06-28T10:30:36.120 回答