java_user
java_user

Reputation: 939

How to make input field and select in one by jQuery or JSP(JSTL) or Spring Forms tag

I need to make input field and selector in one.

I have :

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

And I also have:

<form:input>

So when a user tries to select something from <form:select> and does not find, it he will just put it by himself to the following tag <form:input>.

How to realize it using JQuery or JSP-JSTL of Spring form tags? Is this possible or not?

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

Upvotes: 0

Views: 2031

Answers (1)

Lian
Lian

Reputation: 2357

I would use a hidden input to keep the actual value. Listen to the submit event on the form (via 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());
    }
});

And #input_hidden_id will have the name attribute that your server needs.

Upvotes: 1

Related Questions