简体   繁体   English

jQuery.siblings没有找到兄弟姐妹

[英]jQuery.siblings not finding siblings

I have a very peculiar issue with a web system that I'm maintaining. 我要维护的Web系统存在一个非常特殊的问题。 In a view I want to fetch a question number from a label field in my DOM. 在视图中,我想从DOM的标签字段中获取问题编号。 However my code isn't able to find the label element. 但是我的代码找不到标签元素。

This is the script part. 这是脚本部分。

$(document).ready(function () {
                $(function () {
                var result = $(".answer").click(function () {
                    $(this).editable({
                        className: "",
                        saveUrl: "../api/Results/SaveResults",
                        create: function () {
                            var number = $(this).siblings(".number").text();
                            console.log(number);
                            $(this).editable("option", "questionNumber", number);
                        }
                    });
                });
            });
        });

And this is the HTML 这是HTML

<div class="questionDiv">
                    <asp:Label ID="questionNumber1" runat="server" CssClass="number"></asp:Label>
                    <asp:TextBox ID="answer1" Wrap="True" runat="server" CssClass="answer" TextMode="MultiLine" Width="100%"
                            Rows="5" MaxLength="2000" Columns="65"></asp:TextBox>
                </div>

And rendered in the browser: 并在浏览器中呈现:

<span id="questionNumber1" class="number">1</span>
<textarea name="answer1" rows="5" cols="65" id="answer1" class="answer" style="width: 540px; "></textarea>

For some reason, number in the create function becomes an empty string. 由于某种原因,create函数中的数字将成为一个空字符串。 One of the reasons for using $(this) is because there are more than one question per page. 使用$(this)的原因之一是因为每页有多个问题。 The label.text is set from the code behind in the Page_Load() method. label.text是从Page_Load()方法中后面的代码设置的。

I've been trying with a number of different combinations; 我一直在尝试许多不同的组合。

$(this).prev().text()

$(this).parent().children(".number").text()

$(this).parent().children().find(".number").text()

etc. 等等

If I try these combinations in the chrome console, ie. 如果我在Chrome控制台中尝试这些组合,即 $("#answer1").siblings().text() it works perfectly. $("#answer1").siblings().text()完美运行。

Does anyone have any clue as to why this is happening? 有谁知道为什么会这样吗? All the elements should be rendered as it's done in the document.ready() function. 所有元素都应像在document.ready()函数中一样进行渲染。

Thanks in advance. 提前致谢。

Peter 彼得

EDIT: After som more investigation I've noticed that even if I use var number = $("questionNumber1").text(); 编辑:经过更多调查后,我注意到即使使用var number = $("questionNumber1").text(); I just get an empty string. 我只是得到一个空字符串。 Could the problem have something to do with the DOM not being loaded fully. 问题可能与DOM未完全加载有关。 The script block is in an document.ready() so one would guess that the DOM is loaded. 脚本块位于document.ready()因此可能会猜测已加载DOM。

Maybe this inside create method refers to anything else than $(".answer") ? 也许this内部create方法引用了$(".answer")

Try to backup, possibly it helps: 尝试备份,可能有帮助:

var result = $(".answer").click(function() {
    var $this = $(this);
    $this.editable({
        className: "",
        saveUrl: "../api/Results/SaveResults",
        create: function() {
            var number = $this.siblings(".number").text();
            console.log(number);
            $this.editable("option", "questionNumber", number);
        }
    });
});

The problem was that the create: function was wrapping $(this) in another div. 问题是create:函数将$(this)包装在另一个div中。 So, parent().parent().first(".number") solved it. 因此,parent()。parent()。first(“。number”)解决了它。

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

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