带参数的 Jquery Mvc-3 函数
我有这个页面
<script type="text/javascript">
$(document).ready(function () {
$("#delete").click(function () {
if (confirm) {
$("#divSchedules").load('@Url.Content("~/Export/ScheduleDelete/")');
}
else {
return false;
}
});
});
</script>
<table>
<tr>
<td>Description</td>
<td>Schedule</td>
<td> </td>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.Description</td>
<td>@item.Schedule</td>
<td><a href="@Url.Action("ScheduleEdit", new { @id = item.Id })" class="popLink"><img alt="" src="@Url.Content("~/Content/images/icons/edit.gif")" style="border:none;" /></a>
<img alt="" src="@Url.Content("~/Content/images/icons/delete.gif")" style="border:none;" id="delete" /></td>
</tr>
}
</table>
Jquery 函数由 id="delete" 的元素触发,例如 img 标签。
有人可以帮助我吗,我需要这个 Jquery 函数来使用 onclick 传递参数,例如
$(document).ready(function () {
$("#delete").click(function (id) {
if (confirm) {
$("#divSchedules").load('@Url.Content("~/Export/ScheduleDelete/" + id)');
}
else {
return false;
}
});
});
<img alt="" src="@Url.Content("~/Content/images/icons/delete.gif")" style="border:none;" onclick="delete(@item.Id)" />
我在 jquery 上添加了 id 作为参数。我尝试过,但总是出现编译错误“id 不在上下文中”。
有人可以帮忙吗?非常感谢。
I have this page
<script type="text/javascript">
$(document).ready(function () {
$("#delete").click(function () {
if (confirm) {
$("#divSchedules").load('@Url.Content("~/Export/ScheduleDelete/")');
}
else {
return false;
}
});
});
</script>
<table>
<tr>
<td>Description</td>
<td>Schedule</td>
<td> </td>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.Description</td>
<td>@item.Schedule</td>
<td><a href="@Url.Action("ScheduleEdit", new { @id = item.Id })" class="popLink"><img alt="" src="@Url.Content("~/Content/images/icons/edit.gif")" style="border:none;" /></a>
<img alt="" src="@Url.Content("~/Content/images/icons/delete.gif")" style="border:none;" id="delete" /></td>
</tr>
}
</table>
The Jquery function there is being triggered by elements with id="delete", for instance, a img tag.
Can someone help me please, I need to have this Jquery function to have a parameter passed using onclick like for example
$(document).ready(function () {
$("#delete").click(function (id) {
if (confirm) {
$("#divSchedules").load('@Url.Content("~/Export/ScheduleDelete/" + id)');
}
else {
return false;
}
});
});
<img alt="" src="@Url.Content("~/Content/images/icons/delete.gif")" style="border:none;" onclick="delete(@item.Id)" />
I have added id on jquery as parameter. I tried that but always having compile error "id not in context" thing.
Could someone please help? Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要将其更改
为:
并且您不能使用
id="delete"
拥有多个元素。您应该使用 className 将元素“分组”在一起。 ID 应该是您的服务器的唯一标识符。所以我的想法是为所有删除按钮提供“删除”类,并将单击处理程序附加到该类的所有元素。使用 DOM 对象属性访问可以轻松提取被单击元素的 ID。在代码中:
请记住,ID 不应以数字开头,因此您可能需要也可能不需要使用前缀(我不知道您的 ID 是什么样的)。
I think you need to change this:
to this:
And you can't have multiple elements with
id="delete"
. You should use a className instead to "group" elements together. The IDs should be the unique identifiers from your server. So my thinking is to give all your delete button the 'delete' class, and attach a click handler to all elements of that class. The ID of the clicked element can be easily extracted using DOM object property access.In code:
Keep in mind, that IDs should not start with a number so you may or may not need to use a prefix (I don't know what you're IDs look like).