1

我有 textfiedl,我正在调用下面给出的函数,但问题是它正在输入但不能在 keyup 上工作,或者当文本字段的值发生变化时,我想调用此方法。

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>
4

5 回答 5

2

只需将更改添加到它适用于 keydown 和 change 的功能:

$(函数(){

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('');
        }
    }
});
于 2013-04-04T07:22:19.690 回答
0

使用keyup而不是keydown

 input.on('keyup', function(e) {
   ....
于 2013-04-04T07:20:26.110 回答
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) {

      }
 }
于 2013-04-04T07:22:29.780 回答
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('');
        }
});
于 2013-04-04T07:32:40.297 回答
0
$('#sliderValue170h').change(function(){
{
  //your code here
});
于 2013-04-04T07:17:18.687 回答