gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 23:30:36 24 4
gpt4 key购买 nike

检查此代码,当我单击本地复选框然后警报完成但是当单击 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>

最佳答案

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

代替:

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

你会想要使用:

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

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

关于php - ajax 响应的事件处理程序(多选下拉列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28122015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com