2013-10-16 57 views
0

一個簡單的問題,我試圖根據用戶輸入自動調整輸入文本字段的大小,我對動態創建的輸入文本有一些問題,而其他人正在運行。動態創建的文本輸入不會調整寬度

這裏是JavaScript,我使用:

<script> 
(function($){ 
     $.fn.autoGrowInput = function(o) { 

      o = $.extend({ 
       maxWidth: 1000, 
       minWidth: 0, 
       comfortZone: 70 
      }, o); 

      this.filter('input:text').each(function(){ 

       var minWidth = o.minWidth || $(this).width(), 
        val = '', 
        input = $(this), 
        testSubject = $('<tester/>').css({ 
         position: 'absolute', 
         top: -9999, 
         left: -9999, 
         width: 'auto', 
         fontSize: input.css('fontSize'), 
         fontFamily: input.css('fontFamily'), 
         fontWeight: input.css('fontWeight'), 
         letterSpacing: input.css('letterSpacing'), 
         whiteSpace: 'nowrap' 
        }), 
        check = function() { 

         if (val === (val = input.val())) {return;} 

         // Enter new content into testSubject 
         var escaped = val.replace(/&/g, '&amp;').replace(/\s/g,'&nbsp;').replace(/</g, '&lt;').replace(/>/g, '&gt;'); 
         testSubject.html(escaped); 

         // Calculate new width + whether to change 
         var testerWidth = testSubject.width(), 
          newWidth = (testerWidth + o.comfortZone) >= minWidth ? testerWidth + o.comfortZone : minWidth, 
          currentWidth = input.width(), 
          isValidWidthChange = (newWidth < currentWidth && newWidth >= minWidth) 
               || (newWidth > minWidth && newWidth < o.maxWidth); 

         // Animate width 
         if (isValidWidthChange) { 
          input.width(newWidth); 
         } 

        }; 

       testSubject.insertAfter(input); 

       $(this).bind('keyup keydown blur update', check); 

      }); 

      return this; 

     }; 

    })(jQuery); 

    $('input').autoGrowInput(); 

</script> 

這是我創建的文本輸入字段:

   var tracktitleinput = document.createElement('input'); 
       tracktitleinput.setAttribute('type', "text"); 
       tracktitleinput.setAttribute('name', "tracktitle"); 
       tracktitleinput.setAttribute("required", true); 
       tracktitleinput.setAttribute("placeholder", 
         "Required Field"); 
       tracktitleinput.setAttribute("class", "required"); 

並將其與此的人完全不工作:

<input type="text" id="releasename" name="releasename" required="true" placeholder="Required Field" /> 
+0

配置'.autoGrowInput()'爲動態創建的輸入也 –

+0

是的,這有助於謝謝 – Ducaz035

回答

0

如果他們不存在,當你打電話$('input').autoGrowInput();我不知道你爲什麼期望他們有這個功能屬於他們。 jQuery並不神奇,它不會自動知道當你加載的頁面應用於那些不存在的元素時你運行的代碼,除非你明確告訴它(並且只能通過事件委託對事件起作用) 。

創建動態添加元素時,你需要再次打電話給你的插件:

$(tracktitleinput).autoGrowInput(); 
+0

它不起作用我試過那樣\t \t \t \t \t \t變種tracksubtitleinput =文檔 \t \t \t \t \t \t \t \t .createElement( '輸入'); \t \t \t \t \t \t tracksubtitleinput.setAttribute('type',「text」); \t \t \t \t \t \t tracksubtitleinput.setAttribute('name',「tracksubtitle」); \t \t \t \t \t \t tracksubtitleinput.setAttribute(「onkeyup」,「autoGrowInput()」); \t \t \t \t \t \t $(tracksubtitleinput).autoGrowInput();他們都不起作用 – Ducaz035

+0

@ Ducaz035在調用'$(tracksubtitleinput).autoGrowInput();'之前,嘗試將它添加爲文檔的一部分。 –

+0

太感謝你了,它的作品:) – Ducaz035

1

當你創建新的投入,你需要調用這些元素的autoGrowInput()功能:

var $newInput = $('<input/>', { 
    type : 'text', 
    name : 'tracktitle', 
    required : 'true', 
    placeholder : 'Required Field', 
    class : 'required' 
}).autoGrowInput(); 
+0

我怎樣才能在這個例子中調用,VAR tracksubtitleinput =文件 \t \t \t \t \t \t \t \t .createElement( '輸入'); \t \t \t \t \t \t tracksubtitleinput。setAttribute('type',「text」); \t \t \t \t \t \t tracksubtitleinput.setAttribute('name',「tracksubtitle」);我不知道在用戶輸入之前要創建多少個輸入。 – Ducaz035

+0

@ Ducaz035現在我再看一遍,它看起來像'autoGrowInput'函數可能只有在元素被附加到DOM後才能使用。你可以發佈你如何做到這一點? –

+0

好吧,我現在找到了方法,我應該在追加div之後調用它。謝謝你的幫助 – Ducaz035

相關問題