[英]Working with checkbox which is creating by Ajax
我正在使用ajax項目。 我有一個用戶名,地址,郵政編碼。 在輸入匹配行的名稱,地址或郵政編碼時,由ajax文件顯示。
所以選擇我想要做進一步活動的復選框。
我的HTML代碼是
Address : <input type="text" name="user_name" id="from_location" value="" class="in_form" />
<div id="user"></div>
和jQuery代碼是
$.ajax({
url: "ajax_user.php",
data: {
address: address,
},
dataType: "html",
type: "POST",
success: function(result){
$("#user").append(result);
}
})
}
和ajax用戶的PHP是
$sql= "SELECT * FROM instructor_mst WHERE sex='$sex' AND car_type='$car_type' AND address Like '%$address%' ";
if (!$sqli=mysql_query($sql)){
echo mysql_error();
}
$num_rows= mysql_num_rows($sqli);
if($num_rows != 0)
{?>
<table border="0" class="form_ins" >
<?
while ($row = mysql_fetch_array($sqli))
{
?>
<tr>
<td>
</td>
<td>
Name
</td>
<td>
Address
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="select" value"<?php echo $row['id'];?>">
</td>
<td>
<?php echo $row['name'];?>
</td>
<td>
<?php echo $row['address'];?>
</td>
</tr>
</table
<?}
結果我喜歡
復選框| 用戶名| 地址
現在選擇我要為其他活動提交的復選框....我沒有得到我怎么能這樣做...所有的答案將是appreicieted
隨着您的復選框動態添加 -
$(document).on('change','input[type=checkbox]',function(){
if($(this).is(':checked')){
// do something
}
});
或者如果你有一個復選框的ID -
$(document).on('change','#checkBoxID',function(){
if($(this).is(':checked')){
// do something
}
});
您需要使用on
來委托click事件來動態添加元素,這是您案例中的復選框
$(document).on('click','input[name="select"]',function(){
//this is called when you select the checkbox
//do your stuff
})
或將其委托給最近的靜態元素
$('#user').on('click','input[name="select"]',function(){
//dou your stuff
});
更新
復選框可能有多個值,以便獲取您需要循環遍歷值的所有值或使用map()
嘗試這個
$('#user').on('click','input[name="select"]',function(){
var selectedValue = $("input[name='select']:checked").map(function(n){
return this.value;
});
console.log(selectedValue ); //this will print array in console.
alert(seletedValue.join(',')); //this will alert all values ,comma seperated
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.