Javascript调用仅适用于foreach语句中的第一个项目

时间:2017-09-06 11:19:47

标签: javascript asp.net-mvc

我是mvc的初学者,正在开发一个电子商务网站。 在我的购物车清单中,我需要输入特定数量来计算总价。 我在foreach语句中使用了一个java脚本调用,但该调用仅适用于购物车项目列表中的第一项。

当我点击第二个或其他项目" 总价"然后控件转到第一项" 总价"字段。

我的脚本是

<script>
    function edValueKeyPress() {
        var i ;
        for (i = 0; i < 5; i++) {
            var edValue = document.getElementById("qt");
            var edValue1 = document.getElementById("cprice");
            var s = edValue.value * edValue1.value;
            var t = document.getElementById("total");
            t.value = s;

        }}
 </script>

我的观点

<table class="table">
    @foreach (var item in lscart)
    {
        <tr>
            <td>
                @{
                    var base64 = Convert.ToBase64String(item.CImage);
                    var imgSrc = String.Format("data:image/jpg;base64,{0}", base64);
                    <img src='@imgSrc' style="width:100px; height:100px;" />
                 }
            </td>
            <td>@Html.DisplayFor(modelItem => item.CProductName)</td>
            <td>Rs.</td>
            <td>@Html.EditorFor(modelItem => item.CPrice, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly",@id="cprice" } })</td>

            <td>
                <div class="col-md-6">

                    @Html.EditorFor(modelItem => item.Cquantity, new { htmlAttributes = new { @class = "form-control", @placeholder = " Qty",@id="qt" } })
                </div>

            </td>
            <td>
                <div>
                    @Html.EditorFor(modelItem => item.CTotalPrice, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", @placeholder = " Total Price", @id = "total", @onclick = "edValueKeyPress()", @onkeyup = "edValueKeyPress()" } })

                </div>
            </td>

            <td><a href="#"><i class="fa fa-close"></i></a></td>
        </tr>
    }

</table>

here is the design sample

https://i.stack.imgur.com/MF9pV.png

2 个答案:

答案 0 :(得分:0)

您需要添加相同的ID名称

<table class="table">
    @{
         var i = 0; // add a variable to get count
    }
    @foreach (var item in lscart)
    {
        i++; // incrementing for each iteration
        <tr>
            <td>
                @{
                    var base64 = Convert.ToBase64String(item.CImage);
                    var imgSrc = String.Format("data:image/jpg;base64,{0}", base64);
                    <img src='@imgSrc' style="width:100px; height:100px;" />
                 }
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CProductName)
            </td>
            <td>
                Rs.
            </td>
            <td>
                @Html.EditorFor(modelItem => item.CPrice, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", @id="cprice_"+@i } }) // change id 
            </td>
            <td>
                <div class="col-md-6">
                     @Html.EditorFor(modelItem => item.Cquantity, new { htmlAttributes = new { @class = "form-control", @placeholder = " Qty", @id="qt_"+@i } }) // change id 
                </div>
            </td>
            <td>
                <div>
                    @Html.EditorFor(modelItem => item.CTotalPrice, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", @placeholder = " Total Price", @id = "total_"+@i, @onclick = "edValueKeyPress(@i)", @onkeyup = "edValueKeyPress(@i)" } }) // change id and pass value of i in onclick
                </div>
            </td>
            <td>
                <a href="#"><i class="fa fa-close"></i></a>
            </td>
        </tr>
    }
</table>

脚本将如下所示

<script>
    function edValueKeyPress(i) {
        var edValue = document.getElementById("qt_"+i);
        var edValue1 = document.getElementById("cprice_"+i);
        var s = edValue.value * edValue1.value;
        var t = document.getElementById("total_"+i);
        t.value = s;
    }
</script>

答案 1 :(得分:0)

//查看

<table class="table">

                        @{
                            int i = 0;

                            foreach (var item in lscart)
                            {
                                i = i + 1;
                                <tr>
                                    <td>
                                        @{
                                var base64 = Convert.ToBase64String(item.CImage);
                                var imgSrc = String.Format("data:image/jpg;base64,{0}", base64);
                                <img src='@imgSrc' style="width:100px; height:100px;" />
                                        }
                                    </td>
                                    <td>@Html.DisplayFor(modelItem => item.CProductName)</td>
                                    <td>Rs.</td>
                                    <td>@Html.EditorFor(modelItem => item.CPrice, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", @id = "cprice" + @i } })</td>

                                    <td>
                                        <div class="col-md-6">
                                            @*@Html.EditorFor(modelItem => item.Cquantity, new { htmlAttributes = new { @class = "form-control", @placeholder = " Qty", @onkeyup = "sum()" } })*@
                                            @Html.EditorFor(modelItem => item.Cquantity, new { htmlAttributes = new { @class = "form-control", @placeholder = " Qty", @id = "qt" + @i } })


                                        </div>

                                    </td>


                                    <td>
                                        <div>
                                            @Html.EditorFor(modelItem => item.CTotalPrice, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", @placeholder = " Total Price", @id = "total" + @i, @onclick = "edValueKeyPress(" + @i + ")", @onkeyup = "edValueKeyPress(" + @i + ")" } })

                                        </div>
                                    </td>

                                    <td><a href="#"><i class="fa fa-close"></i></a></td>
                                </tr>
                            }
                        }


                    </table>

//脚本

<script>
    function edValueKeyPress(i) {


        //alert(i);
        var edValue = document.getElementById("qt" + i);
        var edValue1 = document.getElementById("cprice" + i);
        var s = edValue.value * edValue1.value;
        var t = document.getElementById("total" + i);
        t.value = s;






    }
</script>