如何使用javascript调用文本字段中的文本更改功能

时间:2013-04-04 07:14:37

标签: javascript jquery html

我有textfiedl,我正在调用下面给出的一个函数,但问题是它正在处理输入但不处理keyup或者作为textfield更改的值我想调用此方法。

使用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>

5 个答案:

答案 0 :(得分:2)

只需将更改添加到适用于keydown和更改的函数中:

$(function(){

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('');
        }
    }
});

答案 1 :(得分:0)

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

答案 2 :(得分:0)

使用keyup代替keydown

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

答案 3 :(得分: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) {

      }
 }

答案 4 :(得分: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('');
        }
});