我的范围滑块工作正常,但在范围中显示所选范围。除了span,我想要在输入隐藏字段中打印范围,但不能这样做。
我提供了脚本,用 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>
答案 0 :(得分:2)
看看这段代码。它将当前值分配给隐藏字段。您可以通过单击按钮来检查当前值。
$(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>
答案 1 :(得分: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>
* {
-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)
});
});