输入,onchange事件在手动设置值时不会触发

时间:2015-05-17 20:27:38

标签: javascript jquery events

我想在范围滑块上捕捉事件,即使按下按钮并更改值:

$("input").val(1);

http://jsfiddle.net/BaEar/188/

但事件"输入"不按按钮开火。并且"改变"也没有用。

HTML:

<input type="range">
<button>Change</button>
<div id="output"></div>

使用Javascript:

$("input").on("input", function() { //Does not fire on button click :(
    $("#output").text($(this).val());
});

$("button").click(function() {
   $("input").val(1); 
});

有解决方案的人吗?谢谢:))

3 个答案:

答案 0 :(得分:2)

首先,将on处理程序更改为使用change,如

$("input").on("change", function() { 
    $("#output").text($(this).val());
});

其次,在点击处理程序中手动触发它,如

$("button").click(function() {
   $("input").val(1); 
    $("input").trigger("change");
});

fiddle

答案 1 :(得分:1)

我认为com.sun.deploy.net.JARSigningException: Found unsigned entry in resource: http://localhost:8080/<context-root>/client/servlet-api.jar at com.sun.javaws.security.SigningInfo.getCommonCodeSignersForJar(Unknown Source) at com.sun.javaws.security.SigningInfo.check(Unknown Source) at com.sun.javaws.security.JNLPSignedResourcesHelper.checkSignedResourcesHelper(Unknown Source) at com.sun.javaws.security.JNLPSignedResourcesHelper.checkSignedResources(Unknown Source) at com.sun.javaws.Launcher.prepareResources(Unknown Source) at com.sun.javaws.Launcher.prepareAllResources(Unknown Source) at com.sun.javaws.Launcher.prepareToLaunch(Unknown Source) at com.sun.javaws.Launcher.prepareToLaunch(Unknown Source) at com.sun.javaws.Launcher.launch(Unknown Source) at com.sun.javaws.Main.launchApp(Unknown Source) at com.sun.javaws.Main.continueInSecureThread(Unknown Source) at com.sun.javaws.Main.access$000(Unknown Source) at com.sun.javaws.Main$1.run(Unknown Source) at java.lang.Thread.run(Thread.java:745) 应更改为$("input").val(1)。您正在侦听$("input").val(1).trigger('input');事件,因此在更改应该触发事件的值之后。

jsfiddle - &gt; http://jsfiddle.net/BaEar/191/

答案 2 :(得分:1)

试试这个:

$("input").on("input change", function() {
    $("#output").text($(this).val());
});

$("button").click(function() {
   $("input").val(1);
   $("input").change();
});

http://jsfiddle.net/BaEar/192/

因此,范围滑块通常通过更改值进行更新。如果设置了值,则仅触发AmmarCSE解决方案。