【问题标题】:JQuery $(this).find(Selector) returning thisJQuery $(this).find(Selector) 返回 this
【发布时间】:2017-06-23 18:07:27
【问题描述】:

我有一个这样定义的表单

<form name="NewForm">
    <td hidden>New</td>
    <td><input name="name" type="text"></td>
    <td><input name="url" type="url"></td>
    <td><input type="submit" value="New"></td>
    <td><input type="reset" value="Cancel"></td>
</form>

提交时我打电话

function submitForm(event,data)
{
    console.log($(this));


    var valid = true;
    var changed = false

    var input = $(this).find("input[name='name']");

    console.log("Name");
    console.log(input);
    console.log(input.val());
    console.log(input.prop( 'defaultValue' ));
}

控制台然后报告

Object { 0: <form>, context: <form>, length: 1 }
Name
Object { length: 0, prevObject: Object, context: <form>, selector: "input[name='name']" }
undefined
undefined

为什么我没有选择名为 name 的表单上的第一个输入?

编辑:整个代码文件

<!DOCTYPE html>
<html>
<head>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>

<body>
    <div>
        <select id="SystemActive">
            <option value="Y">Active</option>
            <option value="N">Deleted</option>
        </select>
        <button id="SystemRefresh">Refresh</button><br>
        <table id="SystemTable" class="resultsGrid">
            <thead>
                <tr>
                    <th hidden>ID</th>
                    <th>Name</th>
                    <th>URL</th>
                </tr>
                <tr >
                    <form name="NewForm">
                        <td hidden>New</td>
                        <td><input name="name" type="text"></input></td>
                        <td><input name="url" type="url"></input></td>
                        <td><input type="submit" value="New"></input></td>
                        <td><input type="reset" value="Cancel"></input></td>
                    </form>
                </tr>
            </thead>
        </table>
    </div>
    <script>
        $(function() {
            $("form").submit(submitForm)
        });

        function submitForm(event,data)
        {
            console.log($(event.target));


            var valid = true;
            var changed = false

            var input = $(this).find("input[name=name]");
            console.log("Name");
            console.log(input);
            console.log($(input).val());
            input = $(this).find("input[name='name']");
            console.log("Name2");
            console.log(input);
            console.log($(input).val());
            input = $(this).find("input[name='name']");
            console.log("Name3");
            console.log(input.attr('name'));

            event.preventDefault();
            return false;
        }

    </script>

</html>

【问题讨论】:

  • 因为input[name='Name']input[name='name']不一样
  • 表格和选择器大小写都试过了,结果没有区别
  • 区分大小写会有所不同。您的代码无法工作的唯一其他原因是因为this 不是您认为的形式。 submitForm 是如何被调用的?
  • 看一看——这个小提琴(即使使用 jQuery 1.7)再次按照您的预期工作,前提是您适当地绑定了 submit 处理程序。 jsfiddle.net/1kaguv3r
  • 很有可能,这是因为你没有向我们展示你在做什么,你只是认为你在做。请提供一个最低限度的完整且可验证的示例,说明这如何不起作用:stackoverflow.com/help/mcve 包括浏览器和版本(以及您正在使用的jquery 的版本)。

标签: jquery selector


【解决方案1】:

问题是因为form 不是tr 标记的有效子代。只有tdth 可以是tr 标记的子级。浏览器尝试修复您损坏的 HTML,但这样做不正确(与您最初的预期一样),这就是代码无法按预期工作的原因。

要么将表单标签移到table 元素之外,要么将其嵌套在td

只需右键单击并inspect 您的提交元素,您会看到浏览器已修复您的form 标记为没有子元素的元素。

【讨论】:

  • 很好@Adam。
【解决方案2】:

当我将你的表单标签移到表格的一侧时,它开始工作了。

     <form name="NewForm">
        <table id="SystemTable" class="resultsGrid">
            <thead>
                <tr>
                    <th hidden>ID</th>
                    <th>Name</th>
                    <th>URL</th>
                </tr>
                <tr >

                        <td hidden>New</td>
                        <td><input name="name" type="text" /></td>
                        <td><input name="url" type="url" /></td>
                        <td><input type="submit" value="New" /></td>
                        <td><input type="reset" value="Cancel" /></td>

                </tr>
            </thead>
           </table>
        </form>

【讨论】:

  • 和最后一个打开的 有一个额外的空间它应该是
【解决方案3】:

您确实选择了带有语句var input = $(this).find("input[name='name']"); 的第一个名为“name”的输入元素。它按预期工作。请参阅以下内容,其中我可以选择名称为“name”的第一个输入元素并打印其属性。

$('[name="NewForm"]').submit(function( event ) {
   var input = $(this).find("input[name='name']");
  console.log(input.attr('name'));
  event.preventDefault();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="NewForm">
    <td hidden>New</td>
    <td><input name="name" type="text"></td>
    <td><input name="url" type="url"></td>
    <td><input type="submit" value="New"></td>
    <td><input type="reset" value="Cancel"></td>
</form>

【讨论】:

  • 给 OP 一个有效的 sn-p。否则我会展示一个工作演示。
  • 不要使用 onclick 将提交处理程序绑定到表单 - 使用 submit 事件。如果您使用submit 事件,您可以访问this 成为form 元素(OP 已经演示过)。
  • @Adam,不好意思,之前没注意。我更新了我的答案。
  • @VHS 按预期工作不是答案,而是评论。
  • 我将 console.log(input.attr('name')); 添加到了我的提交函数 result = "undefined" 所以除非你能指出你的示例中纠正问题的部分,否则它不起作用
【解决方案4】:

实际上是标记错误。您将form 放入tr。将form 移动到table 之前并在之后关闭它,代码应该可以正常工作。

有效的更新代码位于此 JSFiddle:https://jsfiddle.net/xq940hf1/

【讨论】:

猜你喜欢
  • 2019-12-11
  • 2012-09-20
  • 2014-07-31
  • 1970-01-01
  • 2016-07-23
  • 2017-07-08
  • 2012-05-21
  • 1970-01-01
  • 2014-09-04
相关资源
最近更新 更多