jQuery子元素的变化 [英] Jquery child element on change

查看:65
本文介绍了jQuery子元素的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jquery在子元素(在本例中为select)发生变化时触发事件.

I am trying, using jquery, to trigger an event when a child element, in this case select, changes.

这是我的HTML :

<div id="addForm" ng-repeat="forme in formes" class="row addForm">
    <div class="col-lg-2 col-md-2 col-sm-3 col-xs-6">
        <select id="geo_{{forme.id}}" ng-model="forme.geo" class="form-control">
            <option value="rectangle">Rectangle</option>
            <option value="triangle">Triangle rectangle</option>
            <option value="cercle">cercle</option>
        </select>
    </div>
</div>

尚无法运行的javaScript :

$(document).ready(function () {
    $(document.body).on("change", "#addForm > select", function () {
        alert(this.value);
    });
});

$(document).ready(function () {
    $(document.body).on("change", ".addForm > select", function () {
        alert(this.value);
    });
});

两种方法均无效

这有什么问题?

推荐答案

这不起作用,因为addFormclass而不是id.

This is not working because, addForm is a class not an id.

$(document).on("change", ".addForm select", function () {
   alert(this.value);    
});

使用.classSelector而不是#id选择器.

我也错过了指出另一个错误的机会.也就是说,select不是.addForm的直接子代,它是后代.使用descendant selector代替child selecor.

Also I missed out pointing another one error. That is, select is not a direct child of .addForm it is a descendant. Use descendant selector instead of a child selecor.

这篇关于jQuery子元素的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆