javascript 遍历 div 内的输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24619440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:08:20  来源:igfitidea点击:

Iterate through inputs inside of a div

javascriptjqueryhtmlloopsiteration

提问by Tartar

I'am trying to iterate through all inputs which are placed on a specific div via jQuery, but there is no response. I can't see the values of inputs by using alert. What am I doing wrong ?

我试图遍历所有通过 jQuery 放置在特定 div 上的输入,但没有响应。我无法使用警报查看输入值。我究竟做错了什么 ?

<form id="internshipStage2Form" method="POST" action="form2.php">
    <center>
        <table id="studentInformation" border="1">
            <caption style="color:#f00;">Student(Trainee) Information</caption>
            <tr>
                <td valign="middle" align="center">
                    <label id="stuEmailLabel" for="stuEmailText">E-mail Address</label>
                </td>
                <td valign="middle" align="center"><?php echo $email; ?></td>
            </tr>
            <tr>
                <td valign="middle" align="center">
                    <label id="stuPhoneLabel" for="stuPhoneText">Phone</label>
                </td>
                <td><input id="stuPhoneText" type="text" name="stuPhoneText"/></td>
            </tr>
        </table>
        <div id="companyInfo">
            <table id="companyInformation" border="1">
                <caption style="color:#f00;">Company Information</caption>
                <tr>
                    <td valign="middle" align="center">
                        <label id="companyNameLabel" for="companyNameText">Company Name</label>
                    </td>
                    <td><input id="companyNameText" type="text" name="companyNameText"/></td>
                </tr>
                <tr>
                    <td valign="middle" align="center">
                        <label id="companyAdressLabel" for="companyAdressText">Address</label>
                    </td>
                    <td><input id="companyAdressText" type="text" name="companyAdressText"/></td>
                </tr>
                <tr>
                    <td valign="middle" align="center">
                        <label id="companyPhoneLabel" for="companyPhoneText">Phone</label>
                    </td>
                    <td><input id="companyPhoneText" type="text" name="companyPhoneText"/></td>
                </tr>
                <tr>
                    <td valign="middle" align="center">
                        <label id="" for="">Did the any students work as trainees in the company in the previous years?</label>
                    </td>
                    <td valign="middle" align="center">
                        Yes<input id="g1Positive" type="radio" name="g1" value="YES"/>
                        No<input id="g1Negative" type="radio" name="g1" value="NO"/>
                    </td>
                </tr>
            </table>
        </div>
        <h4 style="color:#f00;">
            I agree the terms.
        </h4>
        <input id="stuDecCheckBox" type="checkbox" name="stuDecCheckBox" /></br>
        <input id="sendButton" type="submit" name="sendButton2" value="SEND FOR APPROVEMENT"/>
    </center>
</form>

JS

JS

$('#companyInfo').children('input').each(function () {
    alert(this.value);
});

Any help would be appriciated.

任何帮助都会受到帮助。

回答by isherwood

Your inputs are not children of #companyInfo. Just do this:

您的输入不是#companyInfo. 只需这样做:

$('#companyInfo input').each(function () {
    alert(this.value);
});

回答by LcSalazar

You can use the findmethod, it looks for nested elements at any level:

您可以使用该find方法,它会在任何级别查找嵌套元素:

$('#companyInfo').find('input').each(function () {
    alert(this.value);
});

http://jsfiddle.net/geLq4/

http://jsfiddle.net/geLq4/

回答by John Bupit

The inputs are not direct descendants of #companyInfo. Try thisinstead:

inputs为不能直接后代#companyInfo试试这个

$('#companyInfo input').each(function () {
    console.log($(this).val());
});

回答by Wes King

From the jQuery documentation:

jQuery 文档

The .children() method differs from .find() in that .children() only travels a single level down the DOM tree

.children() 方法与 .find() 的不同之处在于 .children() 只在 DOM 树中向下移动一层

Classes make a great way to implement multi-element operations quickly.

类是快速实现多元素操作的好方法。

Yes<input class="radioInput" id="g1Positive" type="radio" name="g1" value="YES"/>
No<input class="radioInput" id="g1Negative" type="radio" name="g1" value="NO"/>

Then use .each to iterate by class:

然后使用 .each 按类迭代:

$(document).ready(function() {
    $('body').on('click', '#sendButton', function() {
        $(' .radioInput ').each(function() {
            alert(this.value);
        });
    });
});

And a jsfiddlefor you. Good luck.

还有一个jsfiddle给你。祝你好运。

EDIT: Wrong fiddle, the link has been fixed.

编辑:错误的小提琴,链接已修复。