2017-10-21 21 views
0

我想要使用「 - 選擇 - 」值更改頁面上任何輸入的顏色。下面的代碼工作的模糊:如何更改某個值的輸入顏色

setTimeout(() => { 
      $(document).ready(function() { 
       $('input').blur(function() { 
        if ($(this).val().indexOf('Select') !== -1) { 
         $(this).css('color', '#6D6E71'); 
        } 
       }); 
      }); 
}, 500); 

但因爲我想改變與在頁面加載值「 - 選擇 - 」任何輸入的顏色,我改變了代碼:

setTimeout(() => { 
       $(document).ready(function() {     
         if ($('input').val().indexOf('Select') !== -1) { 
          $(this).css('color', '#6D6E71'); 
         }   
       }); 
}, 500); 

此代碼不起作用。爲什麼不?我應該如何改變它才能工作?任何幫助將不勝感激。

回答

1

您並未檢查$('input')集合中的所有輸入。 $('input').val()只會返回集合中第一個輸入的值。您可以使用each()以遍歷整個集合:

setTimeout(() => { 
    $(document).ready(function() { 
     $('input').each(function() { 
      if ($(this).val().indexOf('Select') !== -1) { 
       $(this).css('color', '#6D6E71'); 
      } 
     }); 
    }); 
}, 500); 
+0

工作正常!非常感謝你! –