JQuery 范围输入侦听器

JQuery Range Input Listener

本文关键字:侦听器 输入 范围 JQuery      更新时间:2023-09-26

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

似乎两者都不起作用。我做错了什么吗?

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

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

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

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

我假设你的跨度有一个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/

更改事件似乎对我正常工作: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);
});​

或者你可以尝试这样的事情:

<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

修复

了几个测试和错误!

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