简体   繁体   English

无法使用javascript从html表中检索除第一行以外的其他行的值

[英]not able to retrieve the value of other rows than the first row from a html table using javascript

I am trying to get the value of a hidden input inside a tag in html table element through javascript in a MVC view. 我试图通过MVC视图中的javascript获取html table元素中的标签内的隐藏输入的值。 i have get the respective value of the hidden input which is in a loop,when the the respective row is clicked. 当单击相应的行时,我已经获得了处于循环中的隐藏输入的相应值。 I have tried many codes but it returns the value of the first row alone for all the rows in the table. 我尝试了许多代码,但是对于表中的所有行,它仅返回第一行的值。 i tried the following: 我尝试了以下方法:

@foreach (var item in Model)

{ {

<tr>
    <td hidden><input value="@item.QuoteId" id="QuoteID" class="QuoteID"> </td>
</tr>
}

javascript: JavaScript的:

$("tr").click(function () {

var quoteid=document.getElementById("#QuoteID").innerHTML alert(quoteid) ; var quoteid=document.getElementById("#QuoteID").innerHTML alert(quoteid) ; alert($('.QuoteID').val()); }

if my db contains 3 values for quote,say 12,17,18.. it alerts 12 for all the row clicks.. Pls help,I am literally stuck. 如果我的数据库包含3个要引用的值,请说12,17,18 ..它会为所有行单击警告12。.请帮助,我确实被卡住了。 I have been trying it from 3 days,i cant figure it out. 我已经尝试了3天,我无法弄清楚。 I guess it is some simple mistake from my side. 我想这是我个人的一些简单错误。 Pls help. 请帮助。 I am not able to finish the work assigned to me because of this simple error. 由于这个简单的错误,我无法完成分配给我的工作。

You're using the same id multiple times. 您多次使用相同的ID。 The ID has to be unique !! 该ID必须是唯一的 To make this to work you could call the unique id, or put a onclick on the specific row and call your function with this . 要使其正常工作,您可以调用唯一的ID,或者在特定的行上单击并使用this调用函数。 In your function you can use this.value . 在您的函数中,您可以使用this.value

<script>
    function ShowMeThePower(myElement) {
        alert(myElement.innerHTML);
    }
</script>
<div onclick="ShowMeThePower(this);">This is great!</div>

http://jsfiddle.net/3RJVd/ http://jsfiddle.net/3RJVd/

Edit: 编辑:

To satisfy the OP: 要满足OP:

<table>
    <tr>
        <td><input type="text" id="show1" value="test1" /></td>
    </tr>
    <tr>
        <td><input type="text" id="show2" value="test2" /></td>
    </tr>
</table>
<script>
    for (var i = 1; i < 3; i++){
        alert(document.getElementById('show' + i).value);
    }
</script>

If you see, it's the same logic. 如果看到的话,这是相同的逻辑。 Just be sure to use unique id's. 只要确保使用唯一的ID。

http://jsfiddle.net/4gMy6/ http://jsfiddle.net/4gMy6/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM