文本框以显示另一个文本框中的值

时间:2014-05-01 10:14:16

标签: javascript html html5

您好我正在尝试将分数文本框值显示为与money文本框相同,因此当money texbox值为10时,我希望得分texbox值更新为10.

任何建议人员。

<form id ="bob">
    <p>money<input name="money" type="text" readonly = "readonly" /></p>
</form>
<form id ="nob">
    <p>score<input name="score" type="text" readonly = "readonly" /></p>
</form>

3 个答案:

答案 0 :(得分:2)

JSFiddle - 为了测试目的,我在readonly ID上删除了money

提供您的HTML输入id

HTML:

<form id ="bob">
  <p>money<input name="money" id="money" type="text" readonly = "readonly" /></p>
</form>
<form id ="nob">
  <p>score<input name="score" id="score" type="text" readonly = "readonly" /></p>
</form>

<button id="but">+ 10</button>

JavaScript的:

var money = document.getElementById("money"),
    score = document.getElementById("score"),
    button = document.getElementById("but");

button.onclick = function(){
  money.value = "10";

  change_score();
};

function change_score(){
  score.value = money.value;
}

答案 1 :(得分:1)

纯JavaScript解决方案:

var money = document.getElementById("money"),
    score = document.getElementById("score");

money.addEventListener("input", function(e){
    score.value = money.value;
});

score.addEventListener("input", function(e){
    money.value = score.value;
});

但您需要在输入字段中添加ID:

<form id ="bob">
    <p>money<input name="money" id="money" type="text"/></p>
</form>
<form id ="nob">
    <p> score <input name="score" id="score" type="text"/></p>
</form>

FIDDLE

答案 2 :(得分:0)

我会使用jquery脚本:

$(document).ready(function(){
    $('[name="money"]').change(function()
    {
        $('[name="score"]').val($(this).val());
    });
});

对于纯JavaScript,这应该有效:

<input name="money" type="text" readonly = "readonly" onchange="function1()" />

<script type="text/javascript>
    function1(){
        document.getElementsByTagName("score")[0].value = document.getElementsByTagName("money")[0].value;
    }
</script>