我有两个按钮和一个文本字段。我希望每次按下按钮时都用按钮值更新文本字段的显示。
function displayTextfield() {
var amount;
amount = document.getElementById('btn').value;
document.getElementById('other-amount') = amount;
}
<button class="btn btn-default" id="btn" onclick='displayTextfield()' role="button" value="A">A</button>
<button class="btn btn-default" id="btn" onclick='displayTextfield()' role="button" value="B">B</button>
<input type="text" class="form-control donation-amount-input" placeholder="Other Amount" id="other-amount" onclick='displayTextfield()'/>
当我运行单击按钮时,我得到了错误:
未捕获值:无法读取null
的属性“”value“”
发布于 2018-10-07 06:07:40
只需向<input type="text" class="form...
添加id="value"
发布于 2018-10-07 06:16:53
点击下面的run代码片段,你可以使用选择器(我用的是btn类)来定位你感兴趣的按钮。然后利用$(this)来获取被点击按钮的值
$('.btn').click(function() {
$('#other-amount').val($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn btn-default" role="button" value="A">A</button>
<button class="btn btn-default" role="button" value="B">B</button>
<input type="text" class="form-control donation-amount-input" placeholder="Other Amount" id="other-amount" />
https://stackoverflow.com/questions/52683717
复制相似问题