繁体   English   中英

在输入隐藏字段jquery中获取输入范围滑块值

[英]Get Input range slider value in input hidden field jquery

我的范围滑块工作正常,但显示跨度中的选定范围。 随着跨度,我想将范围打印在“ 输入”隐藏字段中,但不能这样做。

我提供了可打印span范围的脚本。

 $(window).load(function(){ var range = $('.input-range'), value = $('.range-value'); value.html(range.attr('value')); range.on('input', function(){ value.html(this.value); }); });//]]> 
 * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } .range-slider .input-range { -webkit-appearance: none; width: 300px; height: 10px; border-radius: 5px; background: #353535; outline: none; } .range-slider .input-range::-webkit-slider-thumb { -webkit-appearance: none; width: 20px; height: 20px; border-radius: 50%; background: #666; cursor: pointer; -webkit-transition: background .15s ease-in-out; transition: background .15s ease-in-out; } .range-slider .input-range::-webkit-slider-thumb:hover { background: #fff; } .range-slider .input-range:active::-webkit-slider-thumb { background: #fff; } .range-slider .input-range::-moz-range-thumb { width: 20px; height: 20px; border: 0; border-radius: 50%; background: #666; cursor: pointer; -webkit-transition: background .15s ease-in-out; transition: background .15s ease-in-out; } .range-slider .input-range::-moz-range-thumb:hover { background: #fff; } .range-slider .input-range:active::-moz-range-thumb { background: #fff; } .range-slider .range-value { display: inline-block; position: relative; width: 80px; color: #fff; font-size: 16px; font-weight:bold; line-height: 20px; text-align: center; border-radius: 3px; background: #3f3f3f; padding: 5px 10px; margin-left: 7px; } .range-slider .range-value:after { position: absolute; top: 8px; left: -7px; width: 0; height: 0; border-top: 7px solid transparent; border-right: 7px solid #3f3f3f; border-bottom: 7px solid transparent; content: ''; } ::-moz-range-track { background: #353535; border: 0; } input::-moz-focus-inner { border: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form class="et_pb_contact_form clearfix" method="GET" action="zd"> <p> <h5>Amount in INR</h5> <div class="range-slider"> <input class="input-range" type="range" name="amount" value="" min="1000" step="1000" max="1000000"> <span class="range-value"></span> <input type="hidden" class="test" value=""> </div> </p> <button type="submit" class="et_pb_contact_submit et_pb_button">Submit</button> </form> 

看一下这段代码。 它将当前值分配给隐藏字段。 您可以通过单击按钮来检查当前值。

 $(window).load(function(){ var range = $('.input-range'), value = $('.range-value'); value.html(range.attr('value')); range.on('input', function(){ value.html(this.value); // Write value to hidden field $("input.test").val(this.value); }); // Show current hidden input value $("#showValueButton").click(function() { var currentVal = $("input.test").val(); alert("Current hidden input value: " + currentVal); }); });//]]> 
 * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } .range-slider .input-range { -webkit-appearance: none; width: 300px; height: 10px; border-radius: 5px; background: #353535; outline: none; } .range-slider .input-range::-webkit-slider-thumb { -webkit-appearance: none; width: 20px; height: 20px; border-radius: 50%; background: #666; cursor: pointer; -webkit-transition: background .15s ease-in-out; transition: background .15s ease-in-out; } .range-slider .input-range::-webkit-slider-thumb:hover { background: #fff; } .range-slider .input-range:active::-webkit-slider-thumb { background: #fff; } .range-slider .input-range::-moz-range-thumb { width: 20px; height: 20px; border: 0; border-radius: 50%; background: #666; cursor: pointer; -webkit-transition: background .15s ease-in-out; transition: background .15s ease-in-out; } .range-slider .input-range::-moz-range-thumb:hover { background: #fff; } .range-slider .input-range:active::-moz-range-thumb { background: #fff; } .range-slider .range-value { display: inline-block; position: relative; width: 80px; color: #fff; font-size: 16px; font-weight:bold; line-height: 20px; text-align: center; border-radius: 3px; background: #3f3f3f; padding: 5px 10px; margin-left: 7px; } .range-slider .range-value:after { position: absolute; top: 8px; left: -7px; width: 0; height: 0; border-top: 7px solid transparent; border-right: 7px solid #3f3f3f; border-bottom: 7px solid transparent; content: ''; } ::-moz-range-track { background: #353535; border: 0; } input::-moz-focus-inner { border: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form class="et_pb_contact_form clearfix" method="GET" action="zd"> <p> <h5>Amount in INR</h5> <div class="range-slider"> <input class="input-range" type="range" name="amount" value="" min="1000" step="1000" max="1000000"> <span class="range-value"></span> <input type="hidden" class="test" value=""> </div> </p> <button type="submit" class="et_pb_contact_submit et_pb_button">Submit</button> <button type="button" id="showValueButton">Show value of hidden field</button> </form> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="et_pb_contact_form clearfix" method="GET" action="zd">

<p>
<h5>Amount in INR</h5>
<div class="range-slider">
    <input class="input-range" type="range" name="amount" value="" min="1000" step="1000" max="1000000">
    <span class="range-value"></span>
    <input type="hidden" class="test" value="">
</div> 
</p>



<button type="submit" class="et_pb_contact_submit et_pb_button">Submit</button>

</form>

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.range-slider .input-range {
  -webkit-appearance: none;
  width: 300px;
  height: 10px;
  border-radius: 5px;
  background: #353535;
  outline: none;
}
.range-slider .input-range::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #666;
  cursor: pointer;
  -webkit-transition: background .15s ease-in-out;
  transition: background .15s ease-in-out;
}
.range-slider .input-range::-webkit-slider-thumb:hover {
  background: #fff;
}
.range-slider .input-range:active::-webkit-slider-thumb {
  background: #fff;
}
.range-slider .input-range::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border: 0;
  border-radius: 50%;
  background: #666;
  cursor: pointer;
  -webkit-transition: background .15s ease-in-out;
  transition: background .15s ease-in-out;
}
.range-slider .input-range::-moz-range-thumb:hover {
  background: #fff;
}
.range-slider .input-range:active::-moz-range-thumb {
  background: #fff;
}
.range-slider .range-value {
  display: inline-block;
  position: relative;
  width: 80px;
  color: #fff;
  font-size: 16px;
  font-weight:bold;
  line-height: 20px;
  text-align: center;
  border-radius: 3px;
  background: #3f3f3f;
  padding: 5px 10px;
  margin-left: 7px;
}
.range-slider .range-value:after {
  position: absolute;
  top: 8px;
  left: -7px;
  width: 0;
  height: 0;
  border-top: 7px solid transparent;
  border-right: 7px solid #3f3f3f;
  border-bottom: 7px solid transparent;
  content: '';
}

::-moz-range-track {
  background: #353535;
  border: 0;
}

input::-moz-focus-inner {
  border: 0;
}

$(window).load(function() {
var range = $('.input-range').val(),
  $('.range-value').html(range)
$('.test').val(range)

});
});

暂无
暂无

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

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