当元素来自ajax调用时如何触发函数

时间:2017-10-03 12:07:27

标签: jquery html

这是test.html文件,如下



$(document).ready(function(e) {
				$("#get").click( function() {
					$.ajax({
						type: 'GET',
						url: 'temp.html',
						success: function(data){
							$(".month").html(data);
						}
					});
				});
				
				$("#month").change( function() {
					alert($(this).val());
				});	
			
			});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="get">Get Months</button>
<div class="month"></div>
&#13;
&#13;
&#13;

这是temp.html文件,如下所示

&#13;
&#13;
<select id="month">
	<option>--Select Month--</option>
	<option value='1'>Janaury</option>
	<option value='2'>February</option>
	<option value='3'>March</option>
	<option value='4'>April</option>
	<option value='5'>May</option>
	<option value='6'>June</option>
	<option value='7'>July</option>
	<option value='8'>August</option>
	<option value='9'>September</option>
	<option value='10'>October</option>
	<option value='11'>November</option>
	<option value='12'>December</option>
</select>
&#13;
&#13;
&#13;

当用户点击按钮时,它会获得数月的下拉按钮 我只想在用户从下拉列表中更改月份时收到警报。我无法更改temp.html,因为它来自其他服务器。

1 个答案:

答案 0 :(得分:1)

试试这个

&#13;
&#13;
$(document).on("change", "#month", function() { 
  alert($(this).val());
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="month">
	<option>--Select Month--</option>
	<option value='1'>Janaury</option>
	<option value='2'>February</option>
	<option value='3'>March</option>
	<option value='4'>April</option>
	<option value='5'>May</option>
	<option value='6'>June</option>
	<option value='7'>July</option>
	<option value='8'>August</option>
	<option value='9'>September</option>
	<option value='10'>October</option>
	<option value='11'>November</option>
	<option value='12'>December</option>
</select>
&#13;
&#13;
&#13;