简体   繁体   English

如何在 html 页面中显示行值?

[英]How can I display the row values in html page?

I have to display row values in Html page using jquery, but did not find any solution.我必须使用 jquery 在 Html 页面中显示行值,但没有找到任何解决方案。

` `

<HTML>
<head>
<title>My Java Program</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

</head>
<body>
<table id="myTable" border="2">
<tr><td>hello</td><td>gaurav</td><td>Present<input type="radio" value="present" name="rad0>">&nbsp;
        Absent<input type="radio" value="absent" name="rad0"/></td></tr>
        <tr><td>hello</td><td>gaurav</td><td>Present<input type="radio" value="present" name="rad1>">&nbsp;
        Absent<input type="radio" value="absent" name="rad1"/></td></tr>
        <tr><td>hello</td><td>gaurav</td><td>Present<input type="radio" value="present" name="rad2>">&nbsp;
        Absent<input type="radio" value="absent" name="rad2"/></td></tr>
        <tr><td>hello</td><td>gaurav</td><td>Present<input type="radio" value="present" name="rad3>">&nbsp;
        Absent<input type="radio" value="absent" name="rad3"/></td></tr>





</table>
<br>
<button id="mark" onclick="mark">Mark</button>
<p id="out">-----</p>
<script>
            $(document).ready(function (){
                $("#mark").on("click",function(){


                    var vals="";
                    $('#myTable tr').each(function(){
                        vals+=$(this).find('td:eq(0)').text()+",";
                        vals+=$(this).find('td:eq(1)').text()+",";
                        var checked = $(this).find('input:radio:checked').val();
                        vals+=checked;


                    });

                    $('#out').html(vals);



                });


});
            </script>
            </body>
</html>

And my output is我的输出是

hello,gaurav,undefinedhello,gaurav,undefinedhello,gaurav,undefinedhello,gaurav,undefined你好,高拉夫,未定义你好,高拉夫,未定义你好,高拉夫,未定义你好,高拉夫,未定义

I don't know where i am wrong?我不知道我错在哪里?

You need to give the same name to the radio buttons, and ensure that they are checked (the undefined output is because there isn't any radio button checked).您需要为单选按钮指定相同的名称,并确保它们被选中(未定义的输出是因为没有选中任何单选按钮)。

var vals="";

$('#myTable tr').each(function(){
    vals+=$(this).find('td:eq(0)').text()+",";
    vals+=$(this).find('td:eq(1)').text()+",";
    var checked = $(this).find('input:radio:checked').val();
    if(checked)
        vals+=checked + "<br />";
    else
        vals+="not checked <br />";
});

See this fiddle I've built from your example: https://jsfiddle.net/go7jpp9n/1/请参阅我从您的示例中构建的这个小提琴: https : //jsfiddle.net/go7jpp9n/1/

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

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