douzhan5262 2014-09-02 04:55
浏览 44
已采纳

jQuery选择动态元素

I have dynamically created forms in my page. So once a checkbox in that form checked I want to post the form using jQuery. I still can't select the form correctly.

HTML code:

<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:

<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.

PHP code:

<?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
    }
}
?>
  • 写回答

2条回答 默认 最新

  • doujie4050 2014-09-02 05:05
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘