首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在最近的tr中将jquery响应分配给类名的div

在最近的tr中将jquery响应分配给类名的div
EN

Stack Overflow用户
提问于 2019-07-02 17:33:46
回答 1查看 115关注 0票数 0

我希望在选择defect_type的相应行中的类“defects”中显示jquery的响应。jquery请求正常工作,并且接收到响应,但既不能在td部分中显示,也不能在div中显示。我在“成功”一节中编写的代码行不起作用。

代码语言:javascript
复制
<html>
    <body>
        <table>
            <tr>
                <td>
                    <select class="form-control defect_type" onChange="get_defects(this.value)">
                    <option>1</option>
                    <option>2</option>                                                   
                    </select>
                </td>   
                <td class="defects"></td>
        </tr>
        <tr>
            <td>
                <select class="form-control defect_type" onChange="get_defects(this.value)">
                    <option>1</option>
                    <option>2</option>                                                   
                </select>
            </td>   
            <td class="defects"></td>
        </tr>
    </body>
    <script>    
        $(".defect_type").change(function(){
            if(this.value==""){
                alert("Select Defect Type");
                return;
            }

            $.ajax({
                type: 'post',
                url: '../ajax/get_defects.php',
                data: {
                    defect_type: this.value
                },
                success: function (response) {
                    $(this).closest('tr').find('.defects').html(response);  
                    $(this).closest('tr').find('.defects').text(response);
                }
            });
        });
    </script>
</html>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-02 17:48:01

Problem:成功函数中的$(this)不引用更改的元素。它指的是AJAX。

解决方案:将$(this)分配给变量,然后使用变量。

代码语言:javascript
复制
    $(".defect_type").change(function(){
        if(this.value=="") {
          alert("Select Defect Type");
          return;
       }

       var me = $(this);  // <-- create the var

       $.ajax({
           type: 'post',
           url: '../ajax/get_defects.php',
           data: {
                defect_type: this.value
           },
           success: function (response) {
               me.closest('tr').find('.defects').html(response);  // <--use the var here
               me.closest('tr').find('.defects').text(response);  // <--use the var here
           }
       });
    });
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56857518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档