مسج - لا يمكن تعيين الخاصية المغلق في وظيفة رد اياكس

StackOverflow https://stackoverflow.com/questions/875073

سؤال

وأنا أحاول الحصول على وظيفة مسج اياكس الاستدعاء لتحديث لون خلفية خلية الجدول، ولكن لا أستطيع الحصول على عمل.

ولدي البرمجية التالية (التي تنتج أية أخطاء في الحرائق):

$(".tariffdate").click(function () {
   var property_id = $('#property_id').attr("value");
   var tariff_id = $('#tariff_id').attr("value");
   var tariff_date = $(this).attr("id");
   $.post("/admin/properties/my_properties/booking/edit/*", { property_id: property_id, tariff_id: tariff_id, tariff_date:  tariff_date },
function(data){
   var bgcol = '#' + data;
   $(this).css('background-color',bgcol);
   alert("Color Me: " + bgcol);
});

ولقد أضاف التنبيه فقط لتأكيد أنا الحصول على البيانات المتوقعة الظهر (6 أرقام الشفرة السداسية عشرية)، وأنا - ولكن على خلفية خلية مائدتي يرفض بعناد للتغيير.

وجميع خلايا الجدول لديها .tariffdate الدرجة ولكن لديها أيضا ID الفردية.

وكاختبار، حاولت خلق وظيفة تحوم لتلك الفئة:

$(".tariffdate").hover(function () {
   $(this).css('background-color','#ff0000');
});

ما سبق يعمل بشكل جيد - لذلك أنا في حيرة حقا لماذا بلادي وظيفة رد لا يعمل. أي أفكار؟

هل كانت مفيدة؟

المحلول

في لAJAX الانتهاء معالج يتم تغيير مثيل this إلى كائن اياكس. سوف تحتاج إلى حفظ مثيل this إلى كائن واستخدام ذلك الكائن. على سبيل المثال:

$(".tariffdate").click(function () {
   var property_id = $('#property_id').attr("value");
   var tariff_id = $('#tariff_id').attr("value");
   var tariff_date = $(this).attr("id");
   var tariff = $(this);
   $.post("/admin/properties/my_properties/booking/edit/*", 
      { property_id: property_id, tariff_id: tariff_id, tariff_date:  tariff_date },
      function(data) {
         var bgcol = '#' + data;
         tariff.css('background-color',bgcol);
         alert("Color Me: " + bgcol);
      }
   );
});

نصائح أخرى

وتحقق ما هو "هذا" متغير فيكم ظيفة رد اياكس. وأظن انها ليست اشارة الى .tariffdate

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top