简体   繁体   English

使用select onchange事件刷新jquery滑块

[英]Refresh jquery slider with select onchange event

I have a form with several fields, including a jquery slider and a select dropdown box. 我有一个包含几个字段的表单,包括一个jquery滑块和一个select下拉框。 Currently, when I change the selection of the dropdown, I clear the values of several other fields via the following javascript: 当前,当我更改下拉菜单的选择时,我通过以下javascript清除了其他几个字段的值:

function clearVolumes() {
  document.getElementById('size').value = "";
  document.getElementById('amount').value = "";
  document.getElementById('hardwood_testet').value = "";
}

and I call this with the onchange: 我用onchange来称呼它:

<select id="catbiomass" name="categories[]" onchange="clearVolumes()" />

this is working great for me. 这对我来说很棒。 However, there is a jquery slider in the form (that I found via SO) that I am also trying to refresh back to zero with this onchange, but with no luck. 但是,有一个jquery滑块的形式(我通过SO找到),我也试图通过此onchange刷新回零,但是没有运气。 The jquery for the slider is: 滑块的jquery是:

$( "#hardwoodslider" ).slider({
  range: "min",
  min: 0,
  max: 100,
  step: 5,
  value: 0.00,
  slide: function( event, ui ) {
  $( "#hardwood_testet" ).val( ui.value );
  $(ui.value).val($('#hardwood_testet').val());
  }
  });
$("#hardwood_testet").keyup(function() {
  $("#hardwoodslider").slider("value" , $(this).val())
});

what I'm trying is to add the following jquery: 我正在尝试添加以下jquery:

$("#catbiomass").onchange(function() {
  $("#hardwoodslider").slider("value" , 0)
});

this isn't working, when I change the select field the slider isn't being reset to zero. 这是行不通的,当我更改选择字段时,滑块没有重置为零。 I have been reading around and this code should work, so is the problem because of the fact that I am in fact calling two onchange events in the select field, one via javascript and one via jquery? 我一直在阅读,并且这段代码应该可以工作,因此是问题所在,因为我实际上在select字段中调用了两个onchange事件,一个是通过javascript,另一个是通过jquery?

Thanks 谢谢

I've tried two possible solutions and both work fine for me: 我尝试了两种可能的解决方案,两种方法都对我来说效果很好:

  1. Combine both onChange functions 结合两个onChange函数
     $("#catbiomass").onchange(function() { $(“#catbiomass”)。onchange(function(){\n    document.getElementById('size').value = ""; document.getElementById('size')。value =“”;\n    document.getElementById('amount').value = ""; document.getElementById('amount')。value =“”;\n    document.getElementById('hardwood_testet').value = ""; document.getElementById('hardwood_testet')。value =“”;\n    $("#hardwoodslider").slider("value" , 0) $(“#hardwoodslider”)。slider(“ value”,0)\n}); }); 
    or 要么
     function clearVolumes() { 函数clearVolumes(){\n    document.getElementById('size').value = ""; document.getElementById('size')。value =“”;\n    document.getElementById('amount').value = ""; document.getElementById('amount')。value =“”;\n    document.getElementById('hardwood_testet').value = ""; document.getElementById('hardwood_testet')。value =“”;\n    $("#hardwoodslider").slider("value" , 0); $(“#hardwoodslider”)。slider(“ value”,0);\n} } 
  2. Use ".bind" instead of ".onchange": 使用“ .bind”而不是“ .onchange”:
     $("#catbiomass").bind('change', function() { $(“#catbiomass”)。bind('change',function(){\n    $("#hardwoodslider").slider("value" , 0); $(“#hardwoodslider”)。slider(“ value”,0);\n}); }); 

You can check out my demo here . 您可以在此处查看我的演示。

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

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