JQuery函数仅对第一行起作用

JQuery函数仅对第一行起作用,jquery,asp.net-mvc-4,razor,Jquery,Asp.net Mvc 4,Razor,我已经为锚标记声明了一个onclick函数。它工作正常,但函数只对第一行有效 html块 @foreach(var p in Model) { a++; <tr class="rem1" > <input type="hidden" id="mid" value="@p.mid">

我已经为锚标记声明了一个onclick函数。它工作正常,但函数只对第一行有效

html块

@foreach(var p in Model)
                       {

                        a++;

                    <tr class="rem1" >
                        <input type="hidden" id="mid" value="@p.mid">
                        <td class="invert">@a</td>
                        <td class="invert-image">
                            <a href="single_product.html">
                                <img src="@Url.Content(p.img)" alt=" " height="100"  class="img-responsive">
                            </a>
                       </td>

                 <td class="invert">
                            <div class="quantity">
                                <div class="quantity-select">
                                    <div class="entry value-minus" id="minus">&nbsp;</div>
                                    <div class="entry value" id="counter">1</div>


                //Div which the function written for
                                    <div class="entry value-plus active" id="plus" >&nbsp;</div>

                  //end
                        </div>
                            </div>
                        </td>
                        <td class="invert" onclick="as()">@p.mname</td>

                        <td class="invert" id="prize">@p.mprize</td><td style="display:none" id="pri">@p.mprize</td>
                        <td><a  id="close1" class="btn btn-primary" onclick="return onclickFunction(@p.mid)">Remove</a> </td>
                    </tr>
                     }
按下
时,奖品应增加。这些html块位于for循环中。以显示模型中的值。 但它只适用于第一排

当我试着用这样的类名调用 $(“输入值加”)。单击(函数(){……。});该函数甚至不执行


试试这个片段。您需要找到最接近的mid、计数器、pri等元素,这些元素起源于您的单击附近

$('.entry.value-plus.active').click(function(){ 
    var row = $(this).closest("tr");

    var mid = $(row).children('#mid').val();
    var qua = parseInt($(row).children('#counter').text());
    var a =  parseInt($(row).children('#pri').text());
    var upprize = qua * a;
    $(row).children('#prize').text(upprize);

    $.post("/Purchase/cartcountplus", { Mid:mid,Amt:upprize,qty:qua}, function (data) {
        alert("success");
        setTimeout(function () {// wait for 5 secs(2)
            location.reload(); // then reload the page.(3)
        }, 1000);
    });
});

ID必须是唯一的。改用类。@Andreas当我尝试像$(“.entry value plus”)这样的类时。在(“click”,function(){..}上,该函数不可用executing@codeseeker我已经更新了我的答案,你能看一看吗?@ssbarbeeTried。但是当我按+buttonvar quantity=parseInt($(行).children('#count').text()时,数量字段返回空值我使用了这个(我放置了一个id,其中包含了这些div),但是pblm是在计数变为3后才增加的值
$('.entry.value-plus.active').click(function(){ 
    var row = $(this).closest("tr");

    var mid = $(row).children('#mid').val();
    var qua = parseInt($(row).children('#counter').text());
    var a =  parseInt($(row).children('#pri').text());
    var upprize = qua * a;
    $(row).children('#prize').text(upprize);

    $.post("/Purchase/cartcountplus", { Mid:mid,Amt:upprize,qty:qua}, function (data) {
        alert("success");
        setTimeout(function () {// wait for 5 secs(2)
            location.reload(); // then reload the page.(3)
        }, 1000);
    });
});