简体   繁体   English

如何将一个输入框中的值传递给另一个输入框

[英]How Do I pass a value in a input box, to another input box

I am trying to pass values between boxes. 我试图在框之间传递值。

So, When a User types inside of the first text box: 因此,当用户在第一个文本框中键入内容时:

<input type="text" placeholder="Your personal message" id="valbox"></input>
<input type="submit" name="design1" id="butval" value="Choose Design"></input>

Then they click the 'choose design' button, and what they typed in, gets passed to another input text box on the same page. 然后,他们单击“选择设计”按钮,他们输入的内容将传递到同一页面上的另一个输入文本框中。

this is the second input box i want to pass it to. 这是我要传递给的第二个输入框。

<input type="text" class="input-text" name="billing_last_name" id="billing_last_name" placeholder="" value="">

Any help would be much appreciated thank you 任何帮助将不胜感激谢谢

Live Demo 现场演示

Instead of a submit type input use a button type input. 代替submit类型输入,请使用button类型输入。

HTML HTML

<input type="text" placeholder="Your personal message" id="valbox"></input>
<input type="button" name="design1" id="butval" value="Choose Design"></input>
<input type="text" class="input-text" name="billing_last_name" id="billing_last_name" placeholder="" value="">

JS JS

window.onload = function(){
    document.getElementById('butval').onclick = function(){
        document.getElementById('billing_last_name').value = document.getElementById('valbox').value;   
    }
}; 

First add a clicklistener for the submit button and inside that callback pass the text through the elements 首先,为“提交”按钮添加一个clicklistener,然后在该回调函数中通过元素传递文本

document.getElementById("butval").addEventListener("click", function(event){
    var text = document.getElementById("valbox").value;
    document.getElementById("billing_last_name").value = text;
    event.preventDefault();
    return false;
});

this is by far easiest in jquery given 在给定的jQuery中这是最简单的

<input type="text" placeholder="Your personal message" id="valbox"></input>
<input type="submit" name="design1" id="butval" value="Choose Design"></input>

<input type="text" class="input-text" name="billing_last_name" id="billing_last_name" placeholder="" value="">

use a simple 用一个简单的

$("#butval").click(function(event){
    $("#billing_last_name").html("<p>"+$("#valbox").html()+"</p>");
    event.preventDefault();
});

but better change type="submit" to type="button" then you can remove the essentially unnecessary line event.preventDefault(); 但最好将type="submit"更改为type="button"然后可以删除本质上不必要的行event.preventDefault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM