将一个文本框的值分配给另一个 [英] Assigning value of one text box to another

查看:90
本文介绍了将一个文本框的值分配给另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经看过类似问题的答案,但是对于我一生来说,我无法弄清楚自己做错了什么.

Have looked at the answers to similar questions to this, but for the life of me, I can't figure out what I'm doing wrong.

我有两个文本框和一个按钮.将文本添加到第一个文本框并按下按钮时,我想将第一个文本框的值/文本应用于第二个文本框:

I have a two text boxes and a button. When text is added to the first textbox and the button pressed, I want to apply the first textbox's value/text to the second textbox:

<html>
<head>
    <script type="text/javascript" src="jquery.js"></script>
    <script>
        $("#button").click(function() {
            var contents = $("#textbox").val();
            $("#container").val(contents);
        });
    </script>
</head>
<body>
    <input type="text" id="textbox" /> <input type="submit" id="button" value="Press This" />
    <br />
    <input type="text" id="container" />
</body>
</html>

推荐答案

您不必等待DOM变得就绪.您应该这样写:

You're not waiting for the DOM to become ready. You should write something like:

$(document).ready(function() {
    $("#button").click(function() {
        var contents = $("#textbox").val();
        $("#container").val(contents);
    });
});

这篇关于将一个文本框的值分配给另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆