我有一个简单的局部视图,它被Ajax调用,并通过jQuery添加到cshtml页面上。到现在为止,一切都运转良好。
Create.cshtml
<span id="SpanCoverPhoto"></span>
myjQuery.js
$(document).ready(function () {
$('select[id=BusinessList]').before(function () {
loadCoverPhotos($('select[id=BusinessList]'));
});
$('select[id=BusinessList]').change(function () {
loadCoverPhotos($('select[id=BusinessList]'));
});
function loadCoverPhotos(businessList) {
var id = businessList.val();
$.ajax({
url: "/CoverPhotos/Load",
type: "POST",
data: { 'BId': id },
success: function (data) {
$('span[id=SpanCoverPhoto]').html(data);
},
error: function () {
alert("Cover photos not loaded");
}
});
}
});
部分视图:
@model IEnumerable<banaam.Models.CoverPhoto>
<div>
@foreach (var item in Model)
{
<div class="Coverphoto">
<img src="~/Images/BusinessBackground/[email protected]" class="img-thumbnail" style="max-width:120px; max-height:100px;" />
</div>
}
</div>
现在,我想单击一张照片,然后再次使用jQuery处理它。但是,将数据追加到跨度未在“查看页面源”中显示,只是我在使用F12时可以看到它们。
您能否让我知道,我如何单击照片并得到jQuery的答复?
致以最诚挚的问候,感谢您的宝贵时间,
阿敏
您需要在document
jQuery上设置一个侦听器,以便在整个页面上侦听此事件。即使添加了新元素。
您可以这样做:
$(document).on('click', '.your-class', function(event) {
console.log('Image clicked!');
});
将.your-class更改为.img-thumbnail。
并查看该文档:http : //api.jquery.com/on/。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句