如何将隐藏输入字段的值更改为用户在<input>中键入的值?
输入域
<input
type="number"
id="custom-donation-amount"
placeholder="50.00" onfocus="(this.value == '50.00') && (this.value = '')"
onblur="(this.value == '') && (this.value = '50.00')"
/>而隐藏的那个:
<input id="paypal_value" type="hidden" name="amount" value="">Jquery
<script type="text/javascript">
if (!$('#custom-donation-amount').val()) {
($("#paypal_value").val("50.00")) && ($("#custom-donation-amount").val("50.00"));
}
else $("#custom-donation-amount").change(function(){
$("#paypal_value").val($("#custom-donation-amount").val())
});发布于 2016-03-08 02:35:49
委派 输入事件 on number <input>,然后将一个变量赋值给它的值,然后将该变量赋值给hidden <input>。
使用控制台查看hidden <input>实现number <input>'s值。添加了一个<output>来方便地测试hidden <input>'s值。
片段
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>input to input</title>
</head>
<body>
<input type="number" id="inp1" min="50.00" placeholder="50.00" value="50.00" onfocus="(this.value == '50.00') && (this.value = '')" onblur="(this.value == '') && (this.value = '50.00')"/>
<br/>
<label for="inp2">Value of Hidden Input:</label>
<output id="out1"></output>
<input id="inp2" type="hidden" name="inp2" value="">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
$(function() {
$('#inp1').on('input', function(e) {
var input = $('#inp1').val();
$('#inp2').val(input);
// See the results in devtools or...
console.log('inp2: ' + $("#inp2").val());
// On the page itself.
$('#out1').html($('#inp2').val());
});
});
</script>
</body>
</html>
发布于 2016-03-08 02:49:54
如果用户不输入任何内容(或文本框的默认值),我希望您能够显示默认值。
$(document).ready(function(){
$(".input_amt").on("focus", function(){
$(this).val(parseFloat($(this).attr('placeholder')).toFixed(2));
$("#paypal_value").val('');
});
$(".input_amt").on("blur", function(){
$(this).val(parseFloat($(this).val()).toFixed(2));
if(parseFloat($(this).val()) >50)
{
$("#paypal_value").val(parseFloat($(this).val()).toFixed(2));
}
else
{
$(this).val(parseFloat('50.00').toFixed(2));
$("#paypal_value").val(parseFloat('50.00').toFixed(2));
}
});
});将类input_amt添加到用户输入文本框中
https://stackoverflow.com/questions/35857391
复制相似问题