将输入值克隆到另一个输入值 [英] clone input value to another input value

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

问题描述

我正在尝试做的是当我更新名为 foo 的输入文本ID时,它应该在输入文本ID上实时更新,名为 goo 。我已经接近这个解决方案,但我认为它不起作用。请检查。

What I'm trying to do is when I update input text ID called foo it should be updated real time on input text id called goo. I already close to this solution I think but somehow it is not working. please check.

$('#foo').keyup(updatetxt);
$('#foo').keydown(updatetxt);

var foo = $('#foo');

function updatetxt() {
  $('#goo').val(foo);
}

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js" type="text/javascript"></script>

<input type="text" id="foo" value="something">
<input type="text" id="goo" disabled="disabled" value="something">

推荐答案

您要将元素分配给 val()然而你需要更新值,这应该发生在函数中。

You are assigning the element to val() whereas you need to update the value and that should happen into the function.

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script src="http://code.jquery.com/jquery-1.11.1.js" type="text/javascript"></script>
</head>

<body>


  <input type="text" id="foo" value="something">
  <input type="text" id="goo" disabled="disabled" value="something">



  <script type='text/javascript'>
    $('#foo').keyup(updatetxt);
    //$('#foo').keydown(updatetxt);

    //var foo = $('#foo').val();

    function updatetxt() {
      $('#goo').val($('#foo').val());
    }
  </script>

</body>

</html>

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

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