简体   繁体   中英

How Can I Change the range of a range slider when I select an input radio or other?

I have a question and I hope you can help me with this:

  function GetGradeTemp () { var grades = $( "input[name='radios']:checked" ).val(); $( ".c_f" ).text(grades); var value1 = $("#tempMin").val(); var value2 = $("#tempMax").val(); $( "#minT" ).text(value1); $( "#maxT" ).text(value2); } $("input[name='radios']").change( GetGradeTemp ); $("#tempMin").change( GetGradeTemp ); $("#tempMax").change( GetGradeTemp ); GetGradeTemp(); $("#radio1").click( function() { // }); $("#radio2").click( function() { // }); 
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script> <span class="temp">Min</span><span id="minT" class="min"></span><span class="c_f"></span><span class="max">Max</span><span id="maxT" class="max"></span><span class="c_f"></span> <div class="row"> <div class="input-radio col s12 m12 offset-m5 offset-s5" style="margin-bottom: 0;"> <input type="radio" id="radio1" value="C" name="radios" checked> <label clase ="radioLabel" for="radio1">C</label> <br> <input type="radio" id="radio2" name="radios" value="F"> <label clase ="radioLabel" for="radio2">F</label> </div> </div> <div class="row" id="inline2"> <div class="col s12 m12 l12 offset-m3 offset-l4 temp_uv"> <div class="quantity"> <input type="number" id="tempMin" name="temp_min" step="1" min="-60" max="60" placeholder="-5 C" value="-5" /> </div> <div class="line"><strong>—</strong></div> <div class="quantity1"> <input type="number" id="tempMax" name="temp_max" step="1" min="-60" max="60" placeholder="40 C" value="40" /> </div> </div> </div> <br> <div class="row"> <div id="temp_Slider_C"></div> </div> 

in the code above I have two input type radio and two input type number. With the inputs type radio I can select the degree of temperature: °C or °F. In the input type number I can enter the temperature min and max. I would like to do convert the values from °C to °F or from °F to °C by clicking on the input radio options. Example: If I have the temperature in °C and I do a click on the input radio with value=F, the values of temperature must change in the input type numbers... Do you know how Can I do that with JS/jquery ?

Note: C = (F - 32) * 5 / 9 (Celsius)

F = ((C * 9) / 5 ) + 32 (Fahrenheit)

you only need one change() for both radio .. like so

$('[name="radios"]').on('change',function() {
  // get this value for c or f
  var ThisVal = $(this).val().toLowerCase();
  // inputs val
  var value1 = $("#tempMin").val();
  var value2 = $("#tempMax").val();
  if(ThisVal == 'c'){
    $("#tempMin").val((value1 - 32) * 5 / 9);
    $("#tempMax").val((value2 - 32) * 5 / 9);
  }else{
    $("#tempMin").val(((value1 * 9) / 5 ) + 32);
    $("#tempMax").val(((value2 * 9) / 5 ) + 32);
  }
  GetGradeTemp ();
});

See the working code

 function GetGradeTemp () { var grades = $( "input[name='radios']:checked" ).val(); $( ".c_f" ).text(grades); var value1 = $("#tempMin").val(); var value2 = $("#tempMax").val(); $( "#minT" ).text(value1); $( "#maxT" ).text(value2); } $("input[name='radios']").change( GetGradeTemp ); $("#tempMin").change( GetGradeTemp ); $("#tempMax").change( GetGradeTemp ); GetGradeTemp(); $('[name="radios"]').on('change',function() { // get this value for c or f var ThisVal = $(this).val().toLowerCase(); // inputs val var value1 = $("#tempMin").val(); var value2 = $("#tempMax").val(); if(ThisVal == 'c'){ $("#tempMin").val((value1 - 32) * 5 / 9); $("#tempMax").val((value2 - 32) * 5 / 9); }else{ $("#tempMin").val(((value1 * 9) / 5 ) + 32); $("#tempMax").val(((value2 * 9) / 5 ) + 32); } GetGradeTemp(); }); 
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script> <span class="temp">Min</span><span id="minT" class="min"></span><span class="c_f"></span><span class="max">Max</span><span id="maxT" class="max"></span><span class="c_f"></span> <div class="row"> <div class="input-radio col s12 m12 offset-m5 offset-s5" style="margin-bottom: 0;"> <input type="radio" id="radio1" value="C" name="radios" checked> <label clase ="radioLabel" for="radio1">C</label> <br> <input type="radio" id="radio2" name="radios" value="F"> <label clase ="radioLabel" for="radio2">F</label> </div> </div> <div class="row" id="inline2"> <div class="col s12 m12 l12 offset-m3 offset-l4 temp_uv"> <div class="quantity"> <input type="number" id="tempMin" name="temp_min" step="1" min="-60" max="60" placeholder="-5 C" value="-5" /> </div> <div class="line"><strong>—</strong></div> <div class="quantity1"> <input type="number" id="tempMax" name="temp_max" step="1" min="-60" max="60" placeholder="40 C" value="40" /> </div> </div> </div> <br> <div class="row"> <div id="temp_Slider_C"></div> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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