2013-04-04 43 views
1

我有一個textfied我正在調用下面給出的函數,但問題在於它正在處理輸入,但無法在keyup上或作爲文本字段值的變化我想調用這個方法。如何使用java腳本在文本字段中調用textchange上的函數

的Javascript:

$(function() { 

    var input = $('.input'), 
     bar = $('.bar'), 
     bw = bar.width(), 
     percent = bar.find('.percent'), 
     ps = percent.find('span'); 

    input.on('keydown', function(e) { 
     if (e.keyCode == 13) { 
      var t = $(this), 
       val = t.val(); 
      if (val >= 0 && val <= 100) { 
       var w = 100 - val, 
        pw = (bw * w)/100, 
        pa = { 
         height: w + '%' 
        }, 
        cw = (bw - pw)/2, 
        ca = { 
         left: cw 
        } 
       ps.animate(pa); 
       cs.text(val + '%'); 
       circle.animate(ca, function() { 
        circle.removeClass(name) 
       }).addClass(name); 
      } else { 
       alert('range: 0 - 100'); 
       t.val(''); 
      } 
     } 
    }); 

}); 

HTML:

<div class="text"> 
    <input type="text" class="input" value="0" id="sliderValue170h" /> 
</div> 

回答

0
$('#sliderValue170h').change(function(){ 
{ 
    //your code here 
}); 
+0

textfield文字正在改變,而點擊任何其他按鈕點擊 – user2240329 2013-04-04 07:22:22

+0

@ user2240329檢查更新的答案 – 2013-04-04 07:30:04

0

使用keyup代替​​

input.on('keyup', function(e) { 
    .... 
2

只是增加變化,它同時適用於keydo功能Wn和變化:

$(函數(){

var input = $('.input'), 
    bar = $('.bar'), 
    bw = bar.width(), 
    percent = bar.find('.percent'), 
    ps = percent.find('span'); 

input.on('keydown change', function(e) { // in here it works for both keydown and change 
    if (e.keyCode == 13) { 
     var t = $(this), 
      val = t.val(); 
     if (val >= 0 && val <= 100) { 
      var w = 100 - val, 
       pw = (bw * w)/100, 
       pa = { 
        height: w + '%' 
       }, 
       cw = (bw - pw)/2, 
       ca = { 
        left: cw 
       } 
      ps.animate(pa); 
      cs.text(val + '%'); 
      circle.animate(ca, function() { 
       circle.removeClass(name) 
      }).addClass(name); 
     } else { 
      alert('range: 0 - 100'); 
      t.val(''); 
     } 
    } 
}); 
+0

你能否請在我的代碼編輯我沒有得到這 – user2240329 2013-04-04 07:23:53

+0

這是你想要的? – 2013-04-04 07:33:17

0

可以使用onkeypress事件事件是這樣的:

HTML

<div class="text"> 
    <input type="text" class="input" value="0" onkeypress="return runScript(event)" id="sliderValue170h"/> 
    </div> 

的Javascript

function runScript(e) 
{ 
    //Set Your logic 
     if (e.keyCode == 13) { 

     } 
} 
0
$('#sliderValue170h').bind('change keyup',function(){ 

var t = $(this), 
      val = t.val(); 
     if (val >= 0 && val <= 100) { 
      var w = 100 - val, 
       pw = (bw * w)/100, 
       pa = { 
        height: w + '%' 
       }, 
       cw = (bw - pw)/2, 
       ca = { 
        left: cw 
       } 
      ps.animate(pa); 
      cs.text(val + '%'); 
      circle.animate(ca, function() { 
       circle.removeClass(name) 
      }).addClass(name); 
     } else { 
      alert('range: 0 - 100'); 
      t.val(''); 
     } 
}); 
相關問題