简体   繁体   English

使用点击事件信息填充文本字段

[英]filling a text-field with click-event-information

HTML 的HTML

<div id="wrapper">
  <input type="text" id="text">
  <button id="t-1" value="1">1</button>
  <button id="t-2" value="2">2</button>
  <button id="t-3" value="3">3</button>
</div>

JavaScript 的JavaScript

$(function() {

    $('#wrapper').click(function(event) {

      $('#text').append(event.target.value);

    });

  });

Clicking the button does not change the text-field-value! 单击按钮不会更改文本字段值!

Use this: 用这个:

$(document).ready(function(){
    $('#wrapper').click(function(event) {    
      $('#text').val(event.target.value);
    });
});

Remember append is to add HTML content, whereas val is to get and set the input field value. 请记住, append是添加HTML内容,而val是获取和设置输入字段值。

append adds HTML content after the input. append在输入之后添加HTML内容。 What you are looking for (changing the text of the input) is val . 您正在寻找(更改输入文本)的是val

You need to use val() to set value of a textbox. 您需要使用val()设置文本框的值。 append() is used to append a DOM element into another element (like appending dynamic li into ul tags). append()用于将DOM元素附加到另一个元素中(例如,将动态li附加到ul标签中)。 Below is a working snippet 以下是有效的代码段

 $(function() { $('#wrapper').click(function(event) { $('#text').val(event.target.value); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="wrapper"> <input type="text" id="text"> <button id="t-1" value="1">1</button> <button id="t-2" value="2">2</button> <button id="t-3" value="3">3</button> </div> 

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

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