动态添加的HTML内容事件不会被触发

时间:2015-09-08 09:19:15

标签: c# jquery html ajax asp.net-mvc

我的应用程序中有一个搜索产品页面。我为数据绑定产品列表使用了jQuery ajax函数。产品列表中的每个项目都有一个比较复选框。

enter image description here

请参阅下面的我的产品列表html。

呈现HTML

    <div id="productsContainer">
                            <div class="product_list1">
                                <div class="product_image">
                                    <img src="http://www.barco.com/~/media/Images/Products/2011/3D%20display%20management/XDS-150%20%20200%20%20400.jpg">
                                </div>
                                <div class="product_desc">
                                    <h1>TransForm XDS-1100</h1>
                                    <p>Universal and scalable collaboration solution for a 3D stereo display</p>
                                    <div class="add_compare">
                                        <input id="test" 0'="" _id="55a7624778a72623f843d778" cmp="compare" type="checkbox">
                                        <label for="test" 0'="">Add to compare</label>
                                        <div>
                                        </div>
                                        <div class="clear"></div>
                                    </div>
                                </div>
                                <div class="clear"></div>
                            </div>
                            <div class="product_list1">
                                <div class="product_image">
                                    <img src="http://www.barco.com/~/media/Images/Products/Q%20-%20T/TransForm%20N/Transform%20N01NGP324jpgnewer2jpg.jpg">
                                </div>
                                <div class="product_desc">
                                    <h1>TransForm ECU-110</h1>
                                    <p>Preconfigured and easy to use high-res video wall controller</p>
                                    <div class="add_compare">
                                        <input id="test" 0'="" _id="55a75eac78a72623f843d76f" cmp="compare" type="checkbox">
                                        <label for="test" 0'="">Add to compare</label>
                                        <div>
                                        </div>
                                        <div class="clear"></div>
                                    </div>
                                </div>
                                <div class="clear"></div>
                            </div>

                            <div class="clear"></div>
                        </div>

脚本

$("#productsContainer .product_list1 .product_desc .add_compare").on("click", "input:checkbox", function () {

    alert('message ');

});

修改

初始阶段的HTML

 <div id="productsContainer">

 </div>

Json结果

    public JsonResult GetSearchedProducts(SearchProductModel model)
    {
        _productHelper = new ProductHelper();

        var data = _productHelper.GetSearchedProducts(model);

        StringBuilder strbld = new StringBuilder();

        int i = 0;

        foreach (var item in data)
        {

            strbld.Append("<div class=\"product_list1\">");
            strbld.Append(" <div class=\"product_image\"><img src=" + item.Picturepath + " /></div>");
            strbld.Append(" <div class=\"product_desc\">");
            strbld.Append("     <h1>" + item.Name + "</h1>");
            strbld.Append("     <p>" + item.Shortdesc + "</p>");

            strbld.Append("     <div class=\"add_compare\">");
            strbld.Append("         <input id='test'" + i + "' _id=" + item._id.ToString() + "  cmp='compare' type=\"checkbox\"/>");
            strbld.Append("         <label for='test'" + i + "'>Add to compare</label>");
            strbld.Append("         <div>");

            strbld.Append("         </div>");
            strbld.Append("         <div class=\"clear\"></div>");
            strbld.Append("     </div>");

            strbld.Append(" </div>");
            strbld.Append(" <div class=\"clear\"></div>");
            strbld.Append(" </div>");

        }

        strbld.Append("<div class=\"clear\"></div>");

        return Json(strbld.ToString(), JsonRequestBehavior.AllowGet);
    }

当我点击复选框时,它没有被触发。我该如何解决这个问题?

0 个答案:

没有答案