حدد وتعطيل كل حقل إدخال داخل نموذج ، ملفوف في جدول في jQuery

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

سؤال

أنا أقوم بتعطيل نموذج بناءً على خانة اختيار ...

أواجه مشكلة في إضافة السمة المعوقة.

هذا ما حصلت عليه حتى الآن: HTML:

<table id="shipInfoTable">
  <tr>
   <td>Name:</td>
   <td><input type="text" name="name" /></td>
  </tr>
  ...
</table>

معالجة جافا سكريبت/معالجة السمة (jQuery):

$("#shipInfoTable tbody tr td input").each(function(index, item){
    item.attr("disabled", true);
});

خطأ في وحدة التحكم في Chrome Dev:Uncaught TypeError: Object #<an HTMLInputElement> has no method 'attr'

عندما أنبه item في حدود .each() ينبه [object HTMLInputElement]

لست متأكدًا تمامًا من كيفية تحديد عنصر الإدخال بشكل صحيح. ما الخطأ الذي افعله؟

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

المحلول

لن تمنحك الوظيفة كائن jQuery. يجب أن يكون:

$("#shipInfoTable input").each(function(index, item){
    $(item).attr("disabled", true);
});

(لاحظ أنني قمت أيضًا بتبسيط محددك ، فهو لا يزال يعمل)
إذا كنت لا تفعل أي شيء مع كل عنصر ، فسوف يعمل هذا أيضًا:

$("#shipInfoTable input").attr("disabled", true);

نصائح أخرى

....

$("#shipInfoTable tbody tr td input").each(function(){
    $(this).attr("disabled", true);
});

القضية هي عدم وجود $ () في وظيفة .each () ...

$("#shipInfoTable tbody tr td input").each(function(index, item){
    $(item).attr("disabled", true);
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top