$(this)在JQuery UI Autocomplete Widget中使用.class选择器

时间:2022-10-11 14:27:19
        <!--html-->
        <tr>
          <td data-id="54">
            <input type="text" class="suggestexam" />
          </td>
        <tr>
        <tr>
          <td data-id="16">
            <input type="text" class="suggestexam" />
          </td>
        <tr>
        //JQuery
        $(".suggestexam").autocomplete({
        source: function (request, response) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/Employee/GetExamNameSuggestion",
                data: "{'prefix':'" + request.term + "','testval':'"+ $(this).closest('td').attr('data-id') +"'}",
                dataType: "json",
                success: function (data) {
                    response(data.d);
                },
                error: function (result) {
                    alert("Error");
                }
            });
        }
    });

But $(this).closest('td').attr('data-id') returns undefined.

但$(this).closest('td')。attr('data-id')返回undefined。

Any help?

3 个解决方案

#1


2  

<div data-id="54">
    <input type="text" name="tag" class="autosuggest" id="tagid" />
</div>
    <script>
        $(function () {
            $('body').on('focus', ".autosuggest", function () {
                var autosuggestType = $(this).closest('div').attr("data-id");
                $(this).autocomplete({
                    source: function (request, response) {
                        $.ajax({
                            url: "/Home/FetchTagList",
                            data: JSON.stringify({ tag: request.term, exid: autosuggestType }),
                            dataType: "json",
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            dataFilter: function (data) { return data; },
                            success: function (data) {
                                console.log(data.data);
                                response($.map(data.data, function (item) {
                                    return {
                                        value: item.TagName
                                    }
                                }))
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                alert(textStatus);
                            }
                        });
                    },
                    minLength: 1
                });
            });
            //$(".autosuggest").each(function (index, object) { });
        });
    </script>

#2


0  

$('input').focus(function() {

  console.log($(this).parent().closest('td').data('id'))
  
  $(this).val($(this).parent().closest('td').data('id'));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
  <tr>
    <td data-id="54">
      <input type="text" class="suggestexam" />
    </td>
  </tr>
</table>

Try this

#3


0  

See this post.Try this:

看到这篇文章。试试这个:

//JQuery
 var dataId = $(".suggestexam").closest('td').attr('data-id');
 $(".suggestexam").autocomplete({
     source: function(request, response) {
         $.ajax({
             type: "POST",
             contentType: "application/json; charset=utf-8",
             url: "/Employee/GetExamNameSuggestion",
             data: "{'prefix':'" + request.term + "','testval':'" + dataId + "'}",
             dataType: "json",
             success: function(data) {
                 response(data.d);
             },
             error: function(result) {
                 alert("Error");
             }
         });
     }
 });

#1


2  

<div data-id="54">
    <input type="text" name="tag" class="autosuggest" id="tagid" />
</div>
    <script>
        $(function () {
            $('body').on('focus', ".autosuggest", function () {
                var autosuggestType = $(this).closest('div').attr("data-id");
                $(this).autocomplete({
                    source: function (request, response) {
                        $.ajax({
                            url: "/Home/FetchTagList",
                            data: JSON.stringify({ tag: request.term, exid: autosuggestType }),
                            dataType: "json",
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            dataFilter: function (data) { return data; },
                            success: function (data) {
                                console.log(data.data);
                                response($.map(data.data, function (item) {
                                    return {
                                        value: item.TagName
                                    }
                                }))
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                alert(textStatus);
                            }
                        });
                    },
                    minLength: 1
                });
            });
            //$(".autosuggest").each(function (index, object) { });
        });
    </script>

#2


0  

$('input').focus(function() {

  console.log($(this).parent().closest('td').data('id'))
  
  $(this).val($(this).parent().closest('td').data('id'));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
  <tr>
    <td data-id="54">
      <input type="text" class="suggestexam" />
    </td>
  </tr>
</table>

Try this

#3


0  

See this post.Try this:

看到这篇文章。试试这个:

//JQuery
 var dataId = $(".suggestexam").closest('td').attr('data-id');
 $(".suggestexam").autocomplete({
     source: function(request, response) {
         $.ajax({
             type: "POST",
             contentType: "application/json; charset=utf-8",
             url: "/Employee/GetExamNameSuggestion",
             data: "{'prefix':'" + request.term + "','testval':'" + dataId + "'}",
             dataType: "json",
             success: function(data) {
                 response(data.d);
             },
             error: function(result) {
                 alert("Error");
             }
         });
     }
 });