简体   繁体   English

什么事件获取并在jQuery中显示当前滑块值?

[英]What event to get and display current slider value in jQuery?

I try to get and display current jQuery slider value into textfield, when slider changed. 当滑块更改时,我尝试获取当前jQuery滑块值并将其显示到文本字段中。 But what is event in JS to execute script? 但是JS中执行脚本的事件是什么? I have that script: 我有那个脚本:

function add_value_textarea(){
var sliderval = $("#slider").slider("value");
var txtarea = document.getElementById('mytextfield');
return txtarea.value = sliderval;
}

window.onclick = add_value_textarea;

It works when I click at all place in a document but not when click at slider. 当我在文档中的所有位置单击时,它都起作用,但在滑块上单击时,则不能。 I know i can display value using slider div but I need it in textfield. 我知道我可以使用div滑块显示值,但在文本字段中需要它。

What you are looking for is change event of slider. 您正在寻找的是滑块的change事件。

$( "#slider" ).slider({
  change: function( event, ui ) {
      $("#some").text(ui.value);
  }
});

With html 与HTML

<div id="slider"></div>

<input type="text" id="some"/>

See Demo 观看演示

I guess this is what you're looking for. 我想就是您要寻找的。 Specify a listener to the change event when you create your slider (or afterwards but I don't know why you would do that). 在创建滑块时(或之后,但不知道为什么要这么做),请为更改事件指定一个侦听器。
Also, you use jQuery, so you could change all that by this listener: 另外,您使用jQuery,因此可以通过此侦听器更改所有内容:

function(e, ui) {
    $('#mytextfield').val(ui.value);
}
$( "#slider" ).slider({
slide: function( event, ui ) {
        $( "#mytextfield" ).val( ui.value );
      }
    });

The difference of slide is that the value of the textbox will change as you move the slider (without releasing it). 幻灯片的区别在于,文本框的值会随着您移动滑块(而不释放它)而改变。 If you use change the value of the textbox will change only once when you release the slider. 如果使用change,则释放滑块时,文本框的值将仅更改一次。

Another difference is that if you just click the slider (without actually sliding it) the slide event will not trigger whereas the change event will. 另一个区别是,如果只单击滑块(而不实际滑动),则滑动事件将不会触发,而更改事件将触发。

So you got those 2 alternatives. 因此,您有两种选择。 Pick and choose :) 挑选 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM