使用JS变量修改HTML值

时间:2015-05-06 04:08:13

标签: javascript html tags

我有这个功能,它取值“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">

我该怎么做?

感谢。

4 个答案:

答案 0 :(得分:4)

试试这个,它会起作用

 $('#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">

答案 1 :(得分:2)

jQuery的.val()方法既是getter又是setter。

试试这个:

$('input[name="x"]').val($hello);

答案 2 :(得分:1)

$ 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>

答案 3 :(得分:0)

jQuery <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> 方法用于将值应用于input元素:

.splashdiv

demo link

相关问题