AJAX 响应上的事件处理程序(多选下拉列表)


event handler on ajax response(multi-select dropdown)

检查此代码,当我单击本地复选框时,警报完成,但是当单击AJAX的响应时,事件不会发生。 请更正此代码,以便所有复选框都提供单击响应。
这是索引.php

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {	
$(":checkbox").click(function() {
    var xx= this.id;
    alert(xx);
 });
$( ".target" ).change(function() {
var category= $(this).val();
var datastring='category='+category;
 $.ajax({
         type:"POST",
         url:"next_page.php",
         data: datastring,
       dataType: 'html',
    success: function(html) {
 $('#subcat').html(html);
 
    	},
error: function(xhr) {
	if (xhr.statusText!='OK') {
		alert ("Oopsie: " + xhr.statusText);
		
	}      
   }
      });
       return false; 
}); });
</script>
</head>
<body>
<div id="stage2"  class="col-47-50">
<form >
<select class="target" >
<option value=" ">select a value</option>
<option value="1" > Applied Sciences</option>
<option value="2"> Business &amp; Economics</option>
<option value="3">Engineering &amp; Technology </option>
<option value="4">Environmental Sciences</option>
<option value="5"> Humanities &amp; Art </option>
<option value="6"> Law </option>
<option value="7"> Medical and Life Sciences </option>
<option value="8"> Natural Sciences</option>
<option value="9"> Social Sciences </option>
<option value="10"> School</option>
<option value="11"> others </option>
</select>
</form> 
<div id="subcat">
<!======= display on select anything from stage 2 ========== -->
 </div>      </div>
<div class="topbox">
here your selected values
<form class="stream_submit" name="stream_submit">
<div id="finally_stream"></div>
<label for="check1"><input type="checkbox" id="check1" />Marketing</label>
        <input type="checkbox" id="check2" /><label for="check2">Automotive</label>
<button id="stream_submit"> Next</button>
</form>
</div>       
</body>
</html>


这是next_page.php

<script type="text/javascript" src="jquery-1.9.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	$(":checkbox").click(function() {
    var xx= this.id;
    alert(xx);
 });
$( ".stream" ).change(function() {
var category= $(this).val();	
var xx= $(this).find('option:selected').text();
if (category!='create_custom' && category!='') {
var dataAppend= "<input type='checkbox' id='"+category+"' checked=''"+
" value='"+xx+"'>"+"<label for='"+category+ "' >"+ xx +"</label><br>";
$("#finally_stream").append(dataAppend);
}
	});
	
	
	$( "#add_stream" ).click(function() {
var category= $("input[name=custom_stream]").val();
var xx='add_new_stream';
var dataAppend="<label for='"+category+ "' ><input type='checkbox' id='"+category+"' checked=''"+
" value='"+xx+"'>"+ category +"</label><br>";
$("#finally_stream").append(dataAppend);
	});
	
	
//===== dropdown change effect =========
$(".stream" ).change(function() {
var stream= $(this).val();
//alert(stream);
if (stream=='create_custom') {
	$('#create_custom').css("display", "block")
}else {
	$('#create_custom').css("display", "none")
}
});
});
</script>
<form >
<label for=""> Please select a sub category</label><br>
<select class="stream" multiple="multiple">
<option value="" name="select a value">select a value</option>
<option value="A"> a </option>
<option value="B"> b </option>
<option value="create_custom"> create custom </option>
</select><br>
<div id="create_custom" style="display:none"> 
<label for="" >  custom stream </label><br>
<input type="text" name="custom_stream" placeholder=" new custom stream"><br>
<input type="button" value="Add this" id="add_stream">
</div>
<input type="hidden" name="cat_id" value="'.$cat_id.'">
</form>
these are local check box
<label for="check1"><input type="checkbox" id="check1" />Marketing</label>
        <input type="checkbox" id="check2" /><label for="check2">Automotive</label>

您只绑定页面加载时存在的控件。

而不是:

$(':checkbox').click(...) 

您需要使用:

$(document).on('click', ':checkbox', ...)

这将允许您处理在页面加载后创建的复选框的单击事件