单击Javascript按钮,将值添加到文本框中
Javascript on button click add value to text box
这只是一个超级简单的javascript问题,但我无法了解它是如何工作的
需要将脚本输出到输入。
上的原始脚本http://jsfiddle.net/mYuRK/
谢谢!
var theTotal = 0;
$('button').click(function(){
theTotal = Number(theTotal) + Number($(this).val());
$('.tussenstand').text("Total: "+theTotal);
});
$('.tussenstand').text("Total: "+theTotal);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="tussenstand">
<button value="1">1</button>
<button value="2">2</button>
<button value="4">4</button>
<button value="6">6</button>
由于您使用id作为输入,因此需要使用#
而不是.
作为选择器。对于input
,您必须指定值。
因此,使用$('#tussenstand').val(
代替$('.tussenstand').text(
。
var theTotal = 0;
$('button').click(function(){
theTotal = Number(theTotal) + Number($(this).val());
$('#tussenstand').val("Total: "+theTotal);
});
$('#tussenstand').val("Total: "+theTotal);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="tussenstand">
<button value="1">1</button>
<button value="2">2</button>
<button value="4">4</button>
<button value="6">6</button>
在JS中将类选择器更改为id选择器,并使用val()
而不是text()
。
var theTotal = 0;
$('button').click(function(){
theTotal = Number(theTotal) + Number($(this).val());
$('#tussenstand').val("Total: "+theTotal);
});
$('#tussenstand').val("Total: "+theTotal);
val()
是jQuery用来编辑/检索文本框的值。
使用此脚本:
<script>
function insertTextInInputValue(buttonValueIs){
var inputElementIs = document.getElementById("tussenstand");
inputElementIs.value = inputElementIs.value + buttonValueIs;
}
</script>
使用HTML:
<input id="tussenstand">
<button onclick="insertTextInInputValue(1);">1</button>
<button onclick="insertTextInInputValue(2);">2</button>
<button onclick="insertTextInInputValue(3);">3</button>
<button onclick="insertTextInInputValue(4);">4</button>
<button onclick="insertTextInInputValue(5);">5</button>
<button onclick="insertTextInInputValue(6);">6</button>
相关文章:
- Javascript,将文本添加到具有现有文本节点的元素中
- 选中单选框时,将文本添加到文本框中
- 为未标记的文本添加CSS样式
- 如何将表格中的文本添加到使用按钮输入的文本区域
- 有没有办法将mouseOver上的个人文本添加到完整日历中
- 改变“;onClick"这个jQuery的操作从清除输入文本改为将输入文本添加到下面的列表中
- 如何使用JavaScript将链接文本添加到电子邮件中
- 将动态验证文本添加到自定义jQuery验证规则中
- 将文本添加到每个数组元素的开头
- 单击将按钮中的文本添加到带有jQuery或JS的输入框中
- 通过按钮将不同的文本添加到文本区域-不起作用
- 如何为Fabric.js中的文本添加CSS属性
- 如何将预加载程序文本添加到jQuery Cycle 2中
- Javascript Regex exec 在向搜索文本添加属性/引号后冻结
- 如何在所有浏览器中向文本区域中的选定文本添加 标记
- jQuery将文本添加到放置在表格单元格中的图像中
- D3 垂直条形图为标签文本添加换行符
- 单击时从列表中将文本添加到文本框的方法
- 将文本添加到 D3 圆环图的中心
- 如何将非可变文本添加到javascript打印弹出窗口中