1

Please be easy with me, I am trying to solve but could not hence asking for help. I have 2 combo boxes named as first combo box and second combo box in auto.jsp.

I am getting value in a div in auto.jsp by onchange event of first combo box, but the value, i am getting is not populating in the second combo box rather it is being displayed like plain text inside the id combo2. How to populate this data with in that second combo box. I tried a lot but could not do it , any ideas please?

auto.jsp

<script type="text/javascript">
    $(document).ready(function() {
        $("#combo1").change(function() {
            $.get('combo.jsp', { combo1Val : $(this).val() }, function(responseData) {
                $("#combo2").replaceWith(responseData);
            });
        });
    });          
</script>

<body>
    <select id="combo1" name="combo1Val">// After onchange event of this combo box, second box is disappeared and i am getting value 1, how can i display this one inside the option value of second combo box?
        <option value="">select</option>
        <option value="1">One</option>//
    </select>

    <select id="combo2" name="combo2">
        <option value="">select</option>
        <option value="2">Two</option>
    </select>
</body>

combo.jsp

<% 
    String combo1Val=request.getParameter("comboVal");
    out.println(combo1Val);// displaying value 1 in auto.jsp in id combo2
%>

Any ideas please?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
harry
  • 731
  • 10
  • 23
  • 2
    When I suggested using `.replaceWith()` in an answer to one of your other questions, I explicitly mentioned that your jsp needed to return _all_ of the html for a `` would be replaced with the new one (I gave an example of the html too). I believed the intention was for selection of a value in the first combo to repopulate the entire list in the second. So if you return a simple string then of course `.replaceWith()` is going to replace the whole combo with that string. Are you saying you just want to add one option to the existing options in combo2? – nnnnnn Feb 10 '12 at 10:04
  • @nnnnnn, ya i want to populate that value 1 inside second combo box in an option, not by replacing the entire combo box, can you please give me an answer – harry Feb 10 '12 at 10:10
  • append? how can i populate value 1 in that second combobox? – harry Feb 10 '12 at 10:33
  • @Rory McCrossan..can you please give me an idea , how can it be possible? – harry Feb 10 '12 at 10:35

1 Answers1

1

Try replacing this line:

$("#combo2").replaceWith(responseData);

With something like this:

$("#combo2").append(
   $("<option></option>").html(responseData).val(responseData)
);

That should add a new option to the end of the second combo, where both the display text and the value attribute will be set to responseData (since you're not returning separate values for those).

http://www.htmlgoodies.com/primers/jsp/

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • it worked, can you please tell me how you are knowing all these things but why these things are not entering into my brain? – harry Feb 10 '12 at 10:47
  • that link is telling "page not found" – harry Feb 10 '12 at 10:48
  • My apologies, I didn't notice that the question had been deleted (I assume I can still see it because I have enough rep-points). Try http://www.htmlgoodies.com/primers/jsp/ – nnnnnn Feb 10 '12 at 10:58
  • ya good one by the way i am getting ready to ask one new question – harry Feb 10 '12 at 11:02
  • "Nurse, cancel my one o'clock." – nnnnnn Feb 10 '12 at 11:32
  • please have a look at this question and please give me a solution http://stackoverflow.com/questions/9238267/clean-old-options-from-child-combo-box-when-receiving-data-by-json-array-objects – harry Feb 11 '12 at 10:08