Javascript 每个都不在元素数组上工作

Javascript 每个都不在元素数组上工作,javascript,jquery,Javascript,Jquery,我试图找到所有表单输入元素并对它们进行迭代,不幸的是,每个迭代器都不起作用。我已经检查了$inputs变量,它有5个元素 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Home Page - My ASP.NET MVC Application</title> <script src="/Scrip

我试图找到所有表单输入元素并对它们进行迭代,不幸的是,每个迭代器都不起作用。我已经检查了$inputs变量,它有5个元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Home Page - My ASP.NET MVC Application</title>
    <script src="/Scripts/jquery-1.9.1.js"></script>
</head>
<body>


<script>
$(document).on('click', '#test-endpoint', function () {
    var $form = $(this).parents('#test-form').first();
    var $inputs = $form.find('input, textarea');

    $inputs.each(function () {
    //The code never executed
    });
}
</script>

<form action="http://do.convertapi.com/Word2Pdf" method="post" accept-charset="utf-8" enctype="multipart/form-data" id="test-form">

    <input type="button" name="submitButton" id="test-endpoint" value="Submit1">

    <div class="panel panel-warning">
        <div class="panel-heading">
            <h3 class="panel-title">Endpoints</h3>
        </div>
        <div class="panel-body">
            POST   http://do.convertapi.com/Word2Pdf
        </div>
    </div>


    <div class="panel panel-warning">
        <div class="panel-heading">
            <h3 class="panel-title">Authentication</h3>
        </div>
        <div class="panel-body">
            <table class="table">
                <thead>
                    <tr>
                        <th>Parameter</th>
                        <th>Description</th>
                        <th>Authentication</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>
                            <strong>ApiKey</strong>
                            <p>Optional</p>
                        </td>
                        <td>
                            <strong>String</strong>
                            <p>API Key should be passed if you purchased membership with credits. Please login to your control panel to find out your API Key http://www.convertapi.com/a</p>
                        </td>
                        <td>
                            <input type="text" name="ApiKey" placeholder="Optional">
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <div class="panel panel-warning">
        <div class="panel-heading">
            <h3 class="panel-title">Parameters</h3>
        </div>
        <div class="panel-body">

            <table class="table">
                <thead>
                    <tr>
                        <th>Parameter</th>
                        <th>Description</th>
                        <th>Value</th>
                    </tr>
                </thead>
                <tbody>
                        <tr>
                            <td>
                                <strong>File</strong>
                                <p>Required</p>
                            </td>
                            <td>
                                <strong>Binary</strong>
                                <p>Supported source file formats.</p>
                            </td>
                            <td>
                                    <input type="file" name="File">
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <strong>DocumentTitle</strong>
                                <p>Optional</p>
                            </td>
                            <td>
                                <strong>String</strong>
                                <p>Set the title of the generated Pdf file. If value is not set a source document title is used instead.</p>
                            </td>
                            <td>
                                    <input type="text" name="DocumentTitle" placeholder="Optional">
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <strong>DocumentSubject</strong>
                                <p>Optional</p>
                            </td>
                            <td>
                                <strong>String</strong>
                                <p>Set the subject of the generated Pdf file. If value is not set a source document subject is used instead.</p>
                            </td>
                            <td>
                                    <input type="text" name="DocumentSubject" placeholder="Optional">
                            </td>
                        </tr>
                </tbody>
            </table>

        </div>
    </div>

    <div class="panel panel-warning">
        <div class="panel-heading">
            <h3 class="panel-title">Output</h3>
        </div>
        <div id="test-output" class="panel-body">
        </div>
    </div>

</form>

</body>
</html>
我使用jQuery 1.9.1

尝试用$document.ready围绕它,比如:

调用时,您的元素可能不存在。每个?

您都缺少一个


演示:

您引用的代码有语法错误,我看不出$inputs有什么内容:

$(document).on('click', '#test-endpoint', function () {
    var $form = $(this).parents('#test-form').first();
    var $inputs = $form.find('input, textarea');

    $inputs.each(function () {
      alert("Got: " + this.tagName);
    });
}  // <================= Missing ); here
以下是完整的代码:

var $form = $(this).parents('#test-form').first();
var $inputs = $form.find('input, textarea');

$inputs.each(function (  idx, value) {
//The code never executed
});

你能分享html样本吗?它应该可以工作,你能展示你的html标记吗?作为一个侧面,而不是var$form=$this.parents'test-form';可以替换为var$form=$'test-form';若您有多个表单,那个么使用类而不是id,因为id必须是唯一的,那个么使用.nexist,比如var$form=$this.nexist'.testform'@Anton I发布了完整的html。如问题中所述,$inputs元素包含5个元素。是的,但我认为他可能是在控制台中手动检查$inputs,这似乎是可行的,因为他可能在加载页面时在控制台中检查。而且控制台奇怪地是半活动的,不像终端。
$(document).on('click', '#test-endpoint', function () {
    var $form = $(this).parents('#test-form').first();
    var $inputs = $form.find('input, textarea');

    $inputs.each(function () {
      alert("Got: " + this.tagName);
    });
}  // <================= Missing ); here
Uncaught SyntaxError: Unexpected end of input
var $form = $(this).parents('#test-form').first();
var $inputs = $form.find('input, textarea');

$inputs.each(function (  idx, value) {
//The code never executed
});