[英]jQuery .click function not working only for first link
I have simple foreach loop in my razor view: 我的剃刀视图中有简单的foreach循环:
@foreach (var item in Model.materijali) {
<tr>
<td>
@Ajax.ActionLink(item.sifra_materijala,
"DetaljiMaterijala",
"NormativiMaterijala",
new { materijal = item.sifra_materijala.ToString() },
new AjaxOptions { HttpMethod = "GET" },
new { id = "mylink" })
</td>
</tr>
And a jQuery function: 还有一个jQuery函数:
<script type="text/javascript">
$(function () {
$('#mylink').click(function () {
$.post(this.href, function (json) {
var json = $.parseJSON(data.responseText);
$("#sifra_materijala").val(json.sifra_materijala.val())
alert(json.sifra_materijala);
});
return false;
});
});
</script>
This calls controller action and returns some result that I need to show on my partial view. 这将调用控制器动作,并返回一些需要在局部视图中显示的结果。
When I click on any href
link generated by foreach
loop, controller action is regularly called and executed, except on the first one, when nothing happens... 当我单击由
foreach
循环生成的任何href
链接时,会定期调用并执行控制器动作,除了第一个动作没有任何反应时...
Can someone help me with this? 有人可以帮我弄这个吗?
I've never done any dot net programming, let alone used razor views, but as I understand it, @Ajax.ActionLink
generates an <a>
element and the JavaScript that makes the ajax call when the link is clicked . 我从来没有做过任何点网编程,更不用说使用剃刀视图了,但是据我了解,
@Ajax.ActionLink
生成一个<a>
元素和单击链接时进行ajax调用的JavaScript 。
You have added a second ajax call, but for the first <a>
element only. 您已经添加了第二个ajax调用,但是仅用于第一个
<a>
元素。 It applies only to the first one because even though you may have several <a>
elements with id="mylink"
, id
values are supposed to be unique. 它仅适用于第一个,因为即使您可能具有
id="mylink"
几个<a>
元素, id
值也应该是唯一的。 The handler you supply also has an error in it: It references data.responseText
, but data
is undefined. 您提供的处理程序中也有一个错误:它引用
data.responseText
,但data
未定义。
Instead of adding your own click handler, supply an OnSuccess
callback function for the ajaxOptions
parameter for the @Ajax.ActionLink
call. 无需添加您自己的单击处理程序,
ajaxOptions
为@Ajax.ActionLink
调用的ajaxOptions
参数提供OnSuccess
回调函数。 And get rid of the new { id = "mylink" }
parameter. 并摆脱
new { id = "mylink" }
参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.