0

I have a problem with my javascript. First of all here is my code partitions:

<select class="inputTxt" style="width: 120px;" id="yearCombo" name="yearCombo">
 <option value="0">2013</option>
 <option value="1">2012</option>
 <option value="2">2011</option>                
</select>

function searchClicked() {
    var operationField = document.getElementById("<%=FeedbackReportCtrl.PARAM_OPERATION%>");
    operationField.value = "<%=FeedbackReportCtrl.OPERATION_SEARCH%>";  
    var yearFilter = document.getElementById("<%=FeedbackReportCtrl.PARAM_YEARFILTER%>");
    yearFilter.value = document.getElementById("yearCombo").options[document.getElementById("yearCombo").selectedIndex].text;
    var mainForm = document.getElementById("main"); 
    mainForm.submit();  
}

Here what goes wrong is the following; For example, when I choose the year 2011 from the combo box and then hit the search button, it brings me the desired results;however, the selected index of the box returns back to 2013. How can I sustain my selection after search function?

2 Answers2

1

The issue you have isn't a javascript one. When you submit a form you refresh the whole page, removing any client-side (user or javascript) adjustments to it.

It should be set by the php/java that is generating the page you post your form to, to set a selected="selected" or relevant, based on the value you just posted.

In php this would be

if($_POST['year'] == '2013') echo ' selected="selected"';

In java or jsp there are similar ways of doing this. Javascript itself could do the same probably.

Joris Kroos
  • 431
  • 3
  • 17
0

Submitting the form refreshes the page (unless done via AJAX), thus returning to the default selected value, i.e the first one.

To overcome this you need to send along with the form the chosen year - assuming that you are self-submitting - and explicitly mark this year as the selected option.

In PHP Your code would then be something like:

 <?php $year = $_POST['year']; ?>

<select class="inputTxt" style="width: 120px;" id="yearCombo" name="yearCombo">

<?php for ($i=2013;$i>2010;$i--): ?>

 <option value="<?php echo $i; ?>" <?php if ($year==$i) echo "selected"; ?> >
 <?php echo $i; ?>
 </option>

<?php endfor; ?>

</select>
Matanya
  • 6,233
  • 9
  • 47
  • 80