用JS变量修改一个HTML值
Modify an HTML value with JS variable
我有这个函数,它获取 "count" 的值,打印它并将其保存在变量中
<input type="number" id="count" min="1" value="1" style="text-align:center"><br><br>
<script>
$('#count').bind('click keyup', function(){
alert($(this).val());
$hello = $(this).val();
});
</script>
我想获取 $hello 并将其放入输入标签中的值中,类似这样
<input name="x" type="hidden" value="//here would be $hello">
我该怎么做?
谢谢。
jQuery的.val()
方法既是getter又是setter。
试试这个:
$('input[name="x"]').val($hello);
试试这个,它会起作用
$('#count').bind('click keyup', function(){
alert($(this).val());
var hello = $(this).val();
$("#check").val(hello);
});
<input name="x" id="check" type="hidden" value="//here would be hello">
$hello
之后的用户 setter 变量
<input type="number" id="count" min="1" value="1" style="text-align:center"><br><br>
<script>
$('#count').bind('click keyup', function(){
alert($(this).val());
$hello = $(this).val();
$("#x").val($hello);
});
</script>
jQuery val()
方法用于将值应用于输入元素:
<input type="number" id="count" min="1" value="1" style="text-align:center"><br><br>
<input name="x" type="hidden" value="//here would be $hello">
<script>
$('#count').bind('click keyup', function(){
alert($(this).val());
$hello = $(this).val();
$('input[name="x"]').val($hello);
});
</script>
我有这个函数,它获取 "count" 的值,打印它并将其保存在变量中
<input type="number" id="count" min="1" value="1" style="text-align:center"><br><br>
<script>
$('#count').bind('click keyup', function(){
alert($(this).val());
$hello = $(this).val();
});
</script>
我想获取 $hello 并将其放入输入标签中的值中,类似这样
<input name="x" type="hidden" value="//here would be $hello">
我该怎么做?
谢谢。
jQuery的.val()
方法既是getter又是setter。
试试这个:
$('input[name="x"]').val($hello);
试试这个,它会起作用
$('#count').bind('click keyup', function(){
alert($(this).val());
var hello = $(this).val();
$("#check").val(hello);
});
<input name="x" id="check" type="hidden" value="//here would be hello">
$hello
之后的用户 setter 变量 <input type="number" id="count" min="1" value="1" style="text-align:center"><br><br>
<script>
$('#count').bind('click keyup', function(){
alert($(this).val());
$hello = $(this).val();
$("#x").val($hello);
});
</script>
jQuery val()
方法用于将值应用于输入元素:
<input type="number" id="count" min="1" value="1" style="text-align:center"><br><br>
<input name="x" type="hidden" value="//here would be $hello">
<script>
$('#count').bind('click keyup', function(){
alert($(this).val());
$hello = $(this).val();
$('input[name="x"]').val($hello);
});
</script>