The onChange listener of select does not work

 

The onChange listener of select does not work

 

1. The onChange listener of select does not work. Solution to the problem

 

<selectclass="form-control"name="productName"id="product_name">   

< option  value = "" > Please select </ option >

</select>

Under normal circumstances, we directly set the following methods to listen, and we will find that the onChange event does not listen to the selection operation of the select control

<scripttype="text/javascript"> 

 

$("#product_name").change(function(){ 

$("#product_type option[value='"+$('#product_name option:selected').val()+"']").prop("selected",true);

});

 

</script>

The solution is usually to put it in the page loading initialization $(document).ready( function (){});, this part of the code mainly declares the method of "listening events" after the page is loaded

<scripttype="text/javascript"> 

$(document).ready(function() {

$("#product_name").change(function(){ 

$("#product_type option[value='"+$('#product_name option:selected').val()+"']").prop("selected",true);

}); 

 });

<script>

2. Dynamically clear the option options in the select control

 

$("#product_name").empty();

3. Dynamically add the option option in the select control

 

for(var a=0; a<result.length; a++){

$("#product_name").append("<option value='"+result[a].id+"'>"+result[a].productName+"</option>");

$("#product_type").append("<option value='"+result[a].id+"'>"+result[a].productNum+"</option>");

4. Dynamically modify the option method selected in the select control

 

$("#product_type option[value='"+$('#product_name option:selected').val()+"']").prop("selected",true);

 

 

原文链接:http://blog.csdn.net/ahou2468/article/details/72954974

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326236762&siteId=291194637