Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
184
rated 0 times [  185] [ 1]  / answers: 1 / hits: 27902  / 10 Years ago, mon, october 27, 2014, 12:00:00

I have a combobox, How to fire onchange event of the combobox and get a selected value from it.



Here is the Code what i did till now :



<select name=g1 id=select_g1>
<option value=one>one</option>
<option value=two>two</option>
<option value=three>three</option>
</select>

<script>
$(document).ready(function(){

$(select_g1).change(function(){
alert(Handled); // alert is not fired up ...
});
});

</script>


EDIT : What if i have more than one combobox :



<select name=g2>
<option value=one>one</option>
<option value=two>two</option>
<option value=three>three</option>
</select>


Also disable the value selected in combobox g1 in combobox g2.



Could anyone please tell me what is wrong with the code.


More From » jquery

 Answers
1

Rather than trying to use the .change event, please try to use the .on event.



You can try to use it as such:



$('#select_g1').on('change', function (e) {
var optionSelected = $(option:selected, this);
var valueSelected = this.value;
alert(valueSelected);
});


See this here -> http://jsfiddle.net/ocrozmkn/



EDIT :



With respect to your edited question, you can try the following jQuery :



$('select').on('change', function (e) {
$('select option').prop('disabled', false);
var optionSelected = $(option:selected, this);
var valueSelected = this.value;
alert(valueSelected);
$(select option:contains(' + valueSelected + ')).attr(disabled,disabled);
});


See this here ->http://jsfiddle.net/ocrozmkn/1/



Demo for multiple comboboxes



See here -> http://jsfiddle.net/ocrozmkn/6/



Hope this helps!!!


[#68999] Friday, October 24, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryanulyssesb

Total Points: 91
Total Questions: 105
Total Answers: 102

Location: England
Member since Tue, Sep 8, 2020
4 Years ago
ryanulyssesb questions
Sat, Mar 20, 21, 00:00, 3 Years ago
Mon, Sep 14, 20, 00:00, 4 Years ago
Mon, Mar 9, 20, 00:00, 4 Years ago
Sun, Jul 7, 19, 00:00, 5 Years ago
;