微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – ajax响应的事件处理程序(多选下拉列表)

检查此代码,当我点击本地复选框然后警告已完成,但是当点击ajax的响应时,事件不会发生.请更正此代码,以便所有复选框都提供点击响应.
这是index.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', ...)

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐