javascript - Html 表值未从 Jquery 获取

标签 javascript jquery html

我有一个如下所示的 HTML 表。我将从 jquery 访问该值。但是有一个问题

我的 HTML

                        <table class="table" id="examtbl">
                        <thead>
                            <tr>
                                <th>Ex No</th>
                                <th>Result</th>

                            </tr>
                        </thead>
                        <tbody>
                                <tr id="7500">
                                    <td id="examNo">
                                        <input class="form-control col-md-3 examNo" readonly="readonly" type="text" value="7500" />
                                    </td>
                                    <td>
                                        <input class="form-control col-md-3"  type="text" value="76" />
                                    </td>

                                </tr>
                                <tr id="7600">
                                    <td id="examNo">
                                        <input class="form-control col-md-3 examNo" readonly="readonly" type="text" value="7600" />
                                    </td>
                                    <td>
                                        <input class="form-control col-md-3"  type="text" value="66" />
                                    </td>

                                </tr>
                        </tbody>

                    </table>

我的脚本

 $("#examtbl > tbody > tr").each(function () {
       $(this).find('td').find("input").each(function () {
            alert($(".examNo").val()); < -- It hits3 times but everytime it shows me 7500 only.
       });  

   });

** 您能否给我一个如何获取这些值的解决方案。

最佳答案

问题是,你总是调用 $(".examNo") ...这意味着,他总是寻找第一个适合的项目...你必须使用 this 在每个内部循环中:)

$("#examtbl > tbody > tr").each(function () {
       $(this).find('td').find("input").each(function () {
            alert($(this).val()); 
       });  
   });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table" id="examtbl">
    <thead>
        <tr>
            <th>Ex No</th>
            <th>Result</th>
        </tr>
    </thead>
    <tbody>
        <tr id="7500">
            <td id="examNo">
                <input class="form-control col-md-3 examNo" readonly="readonly" type="text" value="7500" />
            </td>
            <td>
                <input class="form-control col-md-3"  type="text" value="76" />
            </td>
        </tr>
        <tr id="7600">
            <td id="examNo">
                <input class="form-control col-md-3 examNo" readonly="readonly" type="text" value="7600" />
            </td>
            <td>
                <input class="form-control col-md-3"  type="text" value="66" />
            </td>
        </tr>
    </tbody>
</table>

关于javascript - Html 表值未从 Jquery 获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40614412/

相关文章:

javascript - React 生命周期方法中安装更新和卸载的顺序是什么?

javascript - 通过 selenium javascriptExecutor 访问影子 DOM 元素( polymer )

javascript - 随机未定义

html - 在不使用基本路径的情况下从 UIWebView 链接到文档中的 css 文件

javascript - 如何从文本渲染react-router Link组件?

javascript - 当用户向下滚动时显示图像

jquery - 有没有办法在循环中使用 jQuery 的 deferred/promise/when/then 功能来进行多个 ajax 调用?

jquery - .show() 和 .hide() 命令无法正常工作

html - 调整窗口大小时将移动的定位 div

html - WordPress 修改正文类