通过单击按钮从输入字段动态添加值到文本区域
Dynamically add values to textarea from input field by button click
我有一个包含 4 个输入字段的输入表单。两个是下拉列表,两个是文本字段。当用户填写字段并单击旁边的+按钮时,将在下面的文本区域中复制所有值并清除4个输入字段。
四个输入字段是:
<select class="small-field" name="type" id="type" >
<option>Laptop</option>
<option>Desktop</option>
<option>Printer</option>
</select>
<select class="small-field" name="brand" id="model" >
<option>HP</option>
<option>Dell</option>
<option>Advantech</option>
</select>
<input class="small-field" type="text" id="model" name="model" placeholder="model">
<input type="text" id="qty" name="quantity" placeholder="qty." style="width: 50px">
<input type="button" id="addthis" style="width: 30px" value="+" class="button">
文本区域如下-
<textarea name="details" id="details"></textarea>
我为此编写的脚本是
$(function() {
$("#addthis").onclick(function('#type') {
$('#details').val(this.value);
});
$("#addthis").onclick(function('#brand') {
$('#details').val(this.value);
});
$("#addthis").onclick(function('#model') {
$('#details').val(this.value);
});
$("#addthis").onclick(function('#qty') {
$('#details').val(this.value);
});
});
文本区域中的最终输出将如下所示-
Type: Laptop , Brand: HP, Model: CQ4587, Qty: 5
Type: Laptop , Brand: Dell, Model: CQ4587, Qty: 3
Type: Laptop , Brand: HP, Model: CQ4587, Qty: 2
Type: Desktop , Brand: HP, Model: CQ4587, Qty: 7
我已经编辑了您的代码,更改了一些 id 并更改了脚本。您有一些具有相同 id 的元素。
<select class="small-field" name="type" id="type" >
<option>Laptop</option>
<option>Desktop</option>
<option>Printer</option>
</select>
<select class="small-field" name="brand" id="model" >
<option>HP</option>
<option>Dell</option>
<option>Advantech</option>
</select>
<input class="small-field" type="text" id="model_value" name="model" placeholder="model">
<input type="text" id="qty" name="quantity" placeholder="qty." style="width: 50px">
<input type="button" id="addthis" style="width: 30px" value="+" class="button">
<textarea name="details" id="details"></textarea>
在脚本中,我获得了每个输入的值并显示在文本区域中。剧本将是
<script>
$("#addthis").click(function(){
$("#details").val(
$("#details").val() +
"Type: "+$("#type").val() +
" , Brand: "+$("#model").val() +
", Model: "+$("#model_value").val() +
", Qty: "+$("#qty").val() +"'n"
);
});
</script>
http://jsfiddle.net/uwgnx2sw/1/
我希望它有所帮助。
为了确保这些值是串联的(并且保留所有以前输入的值),我已将您的 HTML 修改为以下内容(使用 <fieldset>
将相关项目分组在一起,并删除了各种子项的id
属性以进行验证):
<fieldset class="details">
<select class="small-field" name="type">
<option>Laptop</option>
<option>Desktop</option>
<option>Printer</option>
</select>
<select class="small-field" name="brand">
<option>HP</option>
<option>Dell</option>
<option>Advantech</option>
</select>
<input class="small-field" type="text" name="model" placeholder="model">
<input type="text" name="quantity" placeholder="qty." style="width: 50px">
</fieldset>
<input type="button" style="width: 30px" value="+" class="button">
<textarea name="details" id="details"></textarea>
和jQuery:
// binding the click event-handler to the '#addthis' element:
$('#addthis').on('click', function() {
// creating a reference to all <fieldset class="details"> elements:
var fieldsets = $('fieldset.details'),
// getting the last of those <fieldset> elements:
last = fieldsets.last();
// hiding the last one,
last.hide()
// cloning it:
.clone()
// looking to its child-elements' value property:
.children().prop('value', function(i, v) {
// setting the value to the assigned default-value:
return this.defaultValue;
})
// moving back to the previous selection, the cloned-<fieldset>:
.end()
// inserting the clone before the button:
.insertBefore(this)
// showing the cloned <fieldset>:
.show();
// setting the value of the #details element:
$('#details').val(function() {
// creating a map of:
return fieldsets.map(function() {
// ...a map of the children of each of the <fieldset> elements' children:
return $(this).children().map(function() {
// a string of the name, ': ' and values:
return this.name + ': ' + this.value;
// joining that together into an array, joined by ', ':
}).get().join(', ');
// joining those arrays together with newlines:
}).get().join(''n');
});
});
$('#addthis').on('click', function() {
var fieldsets = $('fieldset.details'),
last = fieldsets.last();
last.hide().clone().children().prop('value', function(i, v) {
return this.defaultValue;
}).end().insertBefore(this).show();
$('#details').val(function() {
return fieldsets.map(function() {
return $(this).children().map(function() {
return this.name + ': ' + this.value;
}).get().join(', ');
}).get().join(''n');
});
});
textarea {
display: block;
width: 90%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset class="details">
<select class="small-field" name="type">
<option>Laptop</option>
<option>Desktop</option>
<option>Printer</option>
</select>
<select class="small-field" name="brand">
<option>HP</option>
<option>Dell</option>
<option>Advantech</option>
</select>
<input class="small-field" type="text" name="model" placeholder="model">
<input type="text" name="quantity" placeholder="qty." style="width: 50px">
</fieldset>
<input type="button" id="addthis" style="width: 30px" value="+" class="button">
<textarea name="details" id="details"></textarea>
引用:
- JavaScript:
-
Array.prototype.join()
.
-
- j查询:
-
get()
. -
children()
. -
end()
. -
hide()
. -
insertBefore()
. -
map()
. -
prop()
. -
show()
. -
val()
.
-
相关文章:
- 在highcharts.js中向点弹出窗口动态添加文本
- 向d3圆添加文本
- 如何在已经有图标的滚动到顶部按钮中添加文本
- 如何在背景图像上添加文本
- 如何使用javascript在asp.net网页表单上动态添加文本框
- 如何从DIV数组向DIV添加文本
- 我可以通过javascript在鼠标光标旁边动态添加文本吗?
- 如何使用 jQuery 在文本区域中的每一行添加文本
- 单击向输入添加文本值的链接
- 使用.html()添加文本输入时出现意外行为
- 在d3.js中添加文本标签以强制定向图链接
- 当我尝试添加文本效果时,有东西阻止了JavaScript的运行'手风琴'在html文档中
- 在每个条形图的中间添加文本标签&鼠标悬停事件
- 通过在 JavaScript 中使用正文标签的 ID 向
标签添加文本
- JavaScript在段落中的页面加载后添加文本
- eventListner,用于在另一个文本框获得焦点时添加文本框(vanilla-js)
- 如何根据复选框在输入中添加文本
- Bootstrap 3通过点击按钮添加文本并显示在新选项卡中
- 用户动态添加文本框时,文本框中的echo值
- jqueryTe正在添加文本