[英]jQuery selecting dynamic elements
I have dynamically created forms in my page. 我在页面中动态创建了表单。 So once a checkbox in that form checked I want to post the form using jQuery.
因此,一旦选中该表单中的复选框,我就想使用jQuery发布表单。 I still can't select the form correctly.
我仍然无法正确选择表格。
HTML code: HTML代码:
<tbody>
<tr>
<form id="" class="recmndation">
<td>sdfdsfdsfdsfsdfsdfsdfsd</td>
<td>
<input class="checkboxes" type="checkbox" name="Luxury Resorts" />
</td>
<td>
<input class="checkboxes" type="checkbox" name="Honeymoon Resorts" />
</td>
<td>
<input class="checkboxes" type="checkbox" name="Dive Resorts" />
</td>
<td>
<input class="checkboxes" type="checkbox" name="Surf Resorts" />
</td>
<td>
<input class="checkboxes" type="checkbox" name="Spa Resorts" />
</td>
<td>
<input class="checkboxes" type="checkbox" name="Over-night Resorts" />
</td>
<input type="hidden" name="hotel_id" value="2" />
</form>
</tr>
<tr>
<form id="" class="recmndation">
<td>tests</td>
<td>
<input class="checkboxes" type="checkbox" name="Luxury Resorts" />
</td>
<td>
<input class="checkboxes" type="checkbox" name="Honeymoon Resorts" />
</td>
<td>
<input class="checkboxes" type="checkbox" name="Dive Resorts" />
</td>
<td>
<input class="checkboxes" type="checkbox" name="Surf Resorts" />
</td>
<td>
<input class="checkboxes" type="checkbox" name="Spa Resorts" />
</td>
<td>
<input class="checkboxes" type="checkbox" name="Over-night Resorts" />
</td>
<input type="hidden" name="hotel_id" value="1" />
</form>
</tr>
</tbody>
jQuery code: jQuery代码:
<script>
$('.checkboxes').on('change', function() {
var val = $(this).parents('.recmndation').attr('id');
alert(val);
});
</script>
The alert
message always says "undefined" and there are no errors in the console. alert
消息始终显示“未定义”,并且控制台中没有错误。
PHP code: PHP代码:
<?php
if (!empty($hotel_list)) {
foreach ($hotel_list as $htls) {
?>
<tr>
<form id="<?php $htls->name ?>" class="recmndation">
<td><?php echo $htls->name; ?></td>
<?php
if ($rcmnd_list) {
foreach ($rcmnd_list as $rcl) {
?>
<td><input class="checkboxes" type="checkbox" name="<?php echo $rcl->rec_name; ?>" /></td>
<?php
}
}
?>
<input type="hidden" name="hotel_id" value="<?php echo $htls->hotel_id; ?>" />
</form>
</tr>
<?php
}
}
?>
Just Try this and it should work: 只需尝试一下,它应该可以工作:
<script>
$(document).ready(function(){
$('.checkboxes').on('change', function() {
var val = $(this).closest('.recmndation').attr('id');
OR YOU CAN USE PROP
var val = $(this).closest('.recmndation').prop('id');
alert(val);
});
});
</script>
First of all, 首先,
1) your form id is empty! 1)您的表格ID为空! so, you will get only empty in alert.
因此,您将只有空警报。
Second of all, 第二,
2) please provide <table>
inside <form>
not the other way around. 2)请不要在
<form>
内提供<table>
。
Your code format should be like this: 您的代码格式应如下所示:
<form id="two" class="recmndation">
<table>
<tr>
<td>
<td><input class="checkboxes" type="checkbox" name="Luxury Resorts" /></td>
<td><input class="checkboxes" type="checkbox" name="Honeymoon Resorts" /></td>
<td><input class="checkboxes" type="checkbox" name="Dive Resorts" /></td>
<td><input class="checkboxes" type="checkbox" name="Surf Resorts" /></td>
<td><input class="checkboxes" type="checkbox" name="Spa Resorts" /></td>
<td><input class="checkboxes" type="checkbox" name="Over-night Resorts" /></td>
<td><input type="hidden" name="hotel_id" value="1" /></td>
</tr>
</table>
</form>
<script>
$('.checkboxes').on('change', function() {
var val = $(this).parents('.recmndation').attr('id');
alert(val);
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.