JQuery Range输入监听器

时间:2012-05-06 19:30:38

标签: javascript jquery html5 range

嘿jquery和hmtl5范围有点麻烦。我尝试在更改时获取值,但事件侦听器未捕获它。目前我的代码是:

HTML

html += '<li>Apply Credits: <input type="range"  min="0" max="'+credits+'" name="discount_credits" id="discount_credits" /> <span>'+credits+'</span></li>'

JS是:

$('#discount_credits').mousewheel( function() { 
        alert('it changed!'); 
        alert('new value = ' + $(this).val()); 
    });

我也试过

$('#discount_credits').change( function() { 
            alert('it changed!'); 
            alert('new value = ' + $(this).val()); 
        });

似乎都没有用。我做错了吗?

5 个答案:

答案 0 :(得分:23)

$('input[type=range]').on('input', function () {
    $(this).trigger('change');
});

这会在每个change事件中触发input事件。

答案 1 :(得分:12)

使用<input type="range"> HTML5 change似乎没有任何问题。

请参阅:http://jsfiddle.net/DerekL/BaEar/33/

答案 2 :(得分:4)

我认为你的跨度有一个id:<span id="slidernumber">...</span> 让我们假设您有一些滑块,如果有任何移动,您希望看到文本更改:

<li>
Apply Credits1: 
<input type="range" min="0" max="100" 
name="discount_credits1" id="discount_credits" 
/>
</li>

<li>
Apply Credits2: 
<input type="range" min="0" max="100" 
name="discount_credits2" id="discount_credits" 
/>
</li>

<span id="slidernumber">50</span>

试试这个:

$(document).ready(function(){
  $("[type=range]").change(function(){
    var newval=$(this).val();
    $("#slidernumber").text(newval);
  });
});

http://jsfiddle.net/MahshidZeinaly/CgQ5c/

现在让我们假设您有一些滑块,但是如果移动了特定的滑块,您希望看到文本更改。 (我假设它是因为范围输入在li标签内,你给它起了一个名字):

<li>
Apply Credits1: 
<input type="range" min="0" max="100" 
name="discount_credits1" id="discount_credits" 
/>
<span id="slidernumber">50</span>
</li>

<li>
Apply Credits2: 
<input type="range" min="0" max="100" 
name="discount_credits2" id="discount_credits" 
/>
<span id="slidernumber">50</span>
</li>

试试这个:

$(document).ready(function(){
  $("[type=range]").change(function(){
    var newv=$(this).val();
    $(this).next().text(newv);
  });
});

http://jsfiddle.net/MahshidZeinaly/2pe7P/1/

答案 3 :(得分:1)

on on change事件似乎对我有效:http://jsfiddle.net/zV3xD/7/

<input type="range"  min="0" max="100" name="discount_credits" id="discount_credits" />

<span id="newValue" value="0">0</span>

$('#discount_credits').change( function() {
    var newValue = this.value;        
    $('#newValue').html(newValue);
});​

或者您可以尝试类似this的内容:

<form oninput="result.value=parseInt(a.value)">
    <input type="range" name="a" value="50" /> =
    <output name="result">0</output>
</form>

在此使用output示例:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/output

答案 4 :(得分:-1)

修复了多个测试和错误!

$('input[name=form_range]').change('mousestop',function () {

});