jquery gets the item currently selected in the select box

As for me, I can only remember what I have done in the project. I haven't done anything before, even if I have seen it before, my impression will not be too deep. In recent projects, you need to select which one to click in the drop-down box to get the id of that item. At that time, a piece of code popped up in your mind, as follows:
		$('select option').click(function(){
			var x = $(this).attr('id');
			alert(x);
		})
Seeing nothing wrong, the browser does not report errors Good at . My tears fell when I opened the browser, and there was no response when I clicked on the option. The reason is that the option does not support click events (I didn't really know this before, it seems that ff and opera support it, and there is no test). The following code is fine, and it is supported by mainstream browsers.
		$('select').change(function(){
			var x = $('select option:selected');
			alert(x.attr('id'));
		})
This code is implemented with the help of the change() event. When the content of the select box changes, it determines which option is selected, and then goes to its ID. One drawback is that the value of the select box can be changed to trigger the event, but it does not affect the function. The following article will optimize this drawback.

Guess you like

Origin blog.csdn.net/dizuncainiao/article/details/78143264