Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何设置第一个的HTML<;李>;父ul中的元素_Jquery - Fatal编程技术网

Jquery 如何设置第一个的HTML<;李>;父ul中的元素

Jquery 如何设置第一个的HTML<;李>;父ul中的元素,jquery,Jquery,我在一个页面上做一个简单的jqueryajax更新,我只想用服务器返回的结果(只是一个数字)更新第一个列表元素 我的HTML有点像这样(psuedo): result只是从服务器返回的数字 完整JQuery代码: $('a[name="PushUp"]').on('click', function(event) { $.ajax({ context: this, type: "post", url: "page.cfm"

我在一个页面上做一个简单的jqueryajax更新,我只想用服务器返回的结果(只是一个数字)更新第一个列表元素

我的HTML有点像这样(psuedo):

result
只是从服务器返回的数字

完整JQuery代码:

 $('a[name="PushUp"]').on('click', function(event) {
 $.ajax({
            context: this,
            type: "post",
            url: "page.cfm",
            data: $.param(data),
            dataType: "json",
            beforeSend: function() {
            },
            success: function(result) {
                $(this).parents("ul.product-controls li:first").html(result);
            },
            complete: function() {},
            error: function(a) {
                alert("An error has occured. Please try again.");
            }
        });
            event.preventDefault();
        });

您可以这样使用,使用
最近()获取父
ul
,然后查找
li:first
以获取
ul
内部的
li

注意:-假设
$(this)
是锚定标记的实例

$(this).closest("ul.product-controls").find("li:first").html(result);
修改的ajax调用-

$('a[name="PushUp"]').on('click', function(event) {

  var $this = $(this);

 $.ajax({
            context: $this,
            type: "post",
            url: "page.cfm",
            data: $.param(data),
            dataType: "json",
            beforeSend: function() {
            },
            success: function(result) {
                $this.closest("ul.product-controls").find("li:first").html(result);
            },
            complete: function() {},
            error: function(a) {
                alert("An error has occured. Please try again.");
            }
        });
            event.preventDefault();
        });

关于如何使用
ID
执行此操作的一些方法:

$(".product-controls").children().eq(0).html(result);
如果您使用的是ajax
,那么这应该是ajax请求的对象,而不是超链接。您可以通过使用
console.log(this)
来检查这一点,然后您会发现它引用了哪个DOM元素


尝试使用:第一个子选择器

$("ul.product-controls li:first-child").html(result);

问题是您的
父母()
没有使用正确的选择器,li不是li的父母。您可以使用
parents('ul')
然后查找第一个li或

编辑以匹配范围

$('a[name="PushUp"]').on('click', function(event) {
   var $this = $(this);
   $.ajax({
     ... 
     success:function(result){
        $this.parent().find('li:first-child').html(result);
     }
   });
});

你必须得到父母,然后去第一里,这是一个好办法

$(this).parents('ul').find("li:first").html(result);
更新:

下面是完整的代码:;)


这会管用的,最近的比父母的好。您需要首先找到给定类的第一个ul,然后在其中查找一个子类:)

您可以这样做:

        <ol>
        <li>
            <ul class="product-controls">
            <li>101</li> <!-- I want to update this element -->
            <li><a onclick="testFunction('up',this);">Push Up</a></li>
            <li><a onclick="testFunction('down',this);">Push Down</a></li>
            </ul>
        </li>
        </ol>

        <script>
            function testFunction(passValue,curObj)
            {
                $(curObj).parent('li').parent('ul.product-controls').find("li:first").html(passValue);
            }
        </script>

    • 101
    • 俯卧撑
    • 推倒
  • 函数testFunction(passValue,curObj) { $(curObj).parent('li').parent('ul.product controls').find(“li:first”).html(passValue); }
    显示完整的jquery代码。如您所见,您的代码按原样工作。所以你的问题一定在别的地方,
    $(这个)
    是锚定标记实例,对吗?请共享您的ajax调用,以更清楚地了解问题。@BhushanKawadkar是的,请共享您的ajax调用,以更清楚地了解问题。检查我的答案。@Spokey谢谢你的回答。然而,这对我不起作用。我更新了我的问题以包含更多代码,因为我认为是$(此)部分出错了@volumeone我已经更新了答案,从外观上看,这是一个范围问题,因为
    $(this)
    不是指单击的元素。
    $('a[name="PushUp"]').on('click', function(event) {
       var $this = $(this);
       $.ajax({
         ... 
         success:function(result){
            $this.parent().find('li:first-child').html(result);
         }
       });
    });
    
    $(this).parents('ul').find("li:first").html(result);
    
    $('li.inner a').click(function(){
        var myUL=$(this).parent().parent();
        myUL.find("li:first").html("5");
    });
    
    $('li.inner a').click(function(){
          $('ul.product-controls').find("li:first").html("for example: 5");
    });
    
    $(this).closest("ul.product-controls").find("li:first").html(result);
    
            <ol>
            <li>
                <ul class="product-controls">
                <li>101</li> <!-- I want to update this element -->
                <li><a onclick="testFunction('up',this);">Push Up</a></li>
                <li><a onclick="testFunction('down',this);">Push Down</a></li>
                </ul>
            </li>
            </ol>
    
            <script>
                function testFunction(passValue,curObj)
                {
                    $(curObj).parent('li').parent('ul.product-controls').find("li:first").html(passValue);
                }
            </script>