[英]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
是获取和设置输入字段值。
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.