PDA

View Full Version : ajax generated select list, invalid post form elements


Axel Maroudas
11-26-2009, 12:06 AM
Hi all,

I am facing an issue that is driving me crazy. I have two dependent select boxes. When the user makes a selection on the first then via ajax the second is filled. Then the form is posted. So far so good. The trouble comes when I edit the form. When I make a selection on the first select list, the second is emptied (normally). If I don't make a selection on the second select list and post the form, even though I set the selectedIndex to -1, the data on the server indicates that the value of the second select list is the one that it was prior to the edit. I might have confused you, so I attach the code.
The select boxes

<select id="country" name="country" size="5" onclick="javascript: getCounties()">
<option value="1">Greece</option>
<option value="2">Spain</option>
</select>

<select id="county" name="county" size="5">
<option value="3">NOROESTE</option>
<option value="4">NORESTE</option>
</select>


The ajax call


function getCounties() {
$.getJSON("<%= request.getContextPath() %>/loadCounties.do?format=json&id=" + $('#country').val(),
function(data){
$("#county").children().remove();
$.each(data, function(i,item){
var optn = document.createElement("option");
optn.text = item.value;
optn.value = item.id;
document.getElementById('county').options.add(optn);
});
});

$('#county').attr('selectedIndex', '-1');
}


To make it clear: say that the for loads with values: Country : 2, County: 1. I click on country 1. The counties select list is empties and populated with some data from the ajax call. Then I post the form, without clicking the county select list. The server says that County is 1, even though I have explicitly set it to null (selectedIndex = -1)

I would appreciate any help
thanx
/axel

abduraooft
11-26-2009, 01:34 AM
Add a dummy option like <option value="-1">---Select---</option>

Axel Maroudas
11-26-2009, 08:39 AM
well, I can's because it is a single select box but it has a size of 5.

thanx fro the reply
/axel

Axel Maroudas
11-27-2009, 03:37 PM
Eventually I resolved the issue. It was not ajax or javascript related. The spring framework behind it does not consider single select lists if they are unchanged in the request.

thanx
/axel