проблема $.post в конкретном сценарии Jquery

#javascript #jquery

#javascript #jquery

Вопрос:

Позвольте мне сначала поделиться своей частью jQuery

 $(".btnUpdateInput").click(function() {
            //alert("Update");
            var BldDocumentId = $("#BldDocId").val();

            var BldinstructionId = $("#BldIPInstnId").val();
            var InputFieldId = $("#InputFieldId").val();
            var InputFieldValue = jQuery.trim($(this).parent().prev().text()); //$("#InputFieldValue").val();
            var InputFieldUserValue = jQuery.trim($(this).prev().val()); // $(".user_input_value").val();
            alert(BldDocumentId   ","   BldinstructionId   ","   InputFieldId   ","   InputFieldValue   ","   InputFieldUserValue);

            var postResu<
            alert($(this).get());
            $.post("/Build/UpdateInputField",
                { bldDocId: BldDocumentId, bldInstnId: BldinstructionId, inputFieldId: InputFieldId, inputFieldValue: InputFieldValue, inputFieldUserValue: InputFieldUserValue },
                function(result) {
                postResult = resu<
                    //Either this should function**
                    alert($(this).get()); // returned Object[Object] but expected the clicked button
                    alert($(this).parent().get());// returned null
                    alert($(this).parent().next().get());//returned null
                    alert($(this).parent().next().children().first().get());// returned null

                    // $(this).parent().next().show();
                    // $(this).parent().next().children().first().text(InputFieldUserValue);
                    // $(this).parent().hide();
                });

                alert(postResult);
            //Or this should function**
            if (postResult == true) {
                $(this).parent().next().show();
                $(this).parent().next().children().first().text(InputFieldUserValue);
                $(this).parent().hide();
            }
        });
  

Теперь позвольте мне объяснить мою проблему. Мне нужно показать и скрыть несколько разделов относительно кнопки «btnUpdateInput», которую я нажал. Я попробовал это двумя способами: 1. Я привел следующие строки в успешной части $.post

 $.post("/Build/UpdateInputField",
                { bldDocId: BldDocumentId, bldInstnId: BldinstructionId, inputFieldId: InputFieldId, inputFieldValue: InputFieldValue, inputFieldUserValue: InputFieldUserValue },
                function(result) {
                postResult = resu<
                    //Either this should function**
                    alert($(this).get()); // returned Object[Object] but expected the clicked button
                    alert($(this).parent().get());// returned null
                    alert($(this).parent().next().get());//returned null
                    alert($(this).parent().next().children().first().get());// returned null

                    // $(this).parent().next().show();
                    // $(this).parent().next().children().first().text(InputFieldUserValue);
                    // $(this).parent().hide();
                });
  

2. или извлеките значение postResult, сравните и сделайте то же самое там. Код приведен ниже:

  if (postResult == true) {
                $(this).parent().next().show();
                $(this).parent().next().children().first().text(InputFieldUserValue);
                $(this).parent().hide();
            }
  

Ни то, ни другое не работает для меня. В 1 я не получаю $ (this) в качестве кнопки ‘btnUpdateInput’, которую я нажимаю, и 2. значение postResult не определено, поскольку задержки нет, так что postResult присваивается результату действия post.

Пожалуйста, помогите мне с любым из сценариев.

Комментарии:

1. Было бы более полезно, если бы вы также разместили соответствующий исходный код HTML.

2. что вы действительно хотите от $ (this)?

Ответ №1:

Это элемент в вашем текущем IN. Итак, в РЕЗУЛЬТАТЕ он возвращается.

Итак, в первом методе вы могли бы сделать:

 $(".btnUpdateInput").click(function(e) {
var self = e;
//code
alert($(self).get()); // returned Object[Object] but expected the clicked button
alert($(self).parent().get());// returned null
alert($(self).parent().next().get());//returned null
alert($(self).parent().next().children().first().get());// returned null
}
  

Кажется, self также используется по умолчанию….

Попробуйте это вместо:

 $(".btnUpdateInput").click(function(e) {
var myButton = $(this);
//code
alert($(myButton).get()); // returned Object[Object] but expected the clicked button
alert($(myButton).parent().get());// returned null
alert($(myButton).parent().next().get());//returned null
alert($(myButton).parent().next().children().first().get());// returned null
}
  

Комментарии:

1. все еще получаю тот же результат, что и ранее: (. все предупреждения возвращают null, кроме 1-го