1

In my page there will be 2 select box [item, and sub-item]

<select id="item" name="item">
 <option value="1">Desktop</option>
 <option value="2">Laptop</option>
</select>


<select id="sub_item" name="sub_item">
</select>

When ever the user changes the item select-control, an event will send the selected-value to the server using jQuery and the server page will output something like this

DELL,39000
HP,32000

And the jQuery should add those values to the sub-item select box so it changes to

<select id="sub_item" name="sub_item">
 <option value="39000">Dell</option>
 <option value="32000">HP</option>
</select>

Problem: I can't code how to add/reset the select control for the sub-item

I don't have any code to share. If you want my SQL or JAVA code I can post it, but I think the backend has nothing to do with the jQuery modifying the sub-item's value

3

2 Answers 2

1
$.get('URL',function(data){
  //data should be DELL,39000 osv
  var items = data.split("\n");
  items.forEach(function(item){
    item = item.split(',');
    var option = $("<option></option>").text(item[0]).val(item[1]);
    $("sub_item").append(option);
  });
});

Change 'URL' to your url.

Not tested, but should work.

Sign up to request clarification or add additional context in comments.

Comments

1

assuming this as input, just a plain string

Your server will respond with the string DELL,39000 # HP,32000 ( Here # represents the second item. )

assign it to a variable in javascript

var data = response.split( "#"); // Splits the items

for( i=0; i < data.length; i++){
  $('#sub_item').append( '<option value='+data[i].split(",")[0]+'>'+data[i].split(",")[1]+'</option>');
}

This will append the data to the sub_item select box with the response from the Server

To clear the droddown

$'#sub_item').html('');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.