Javascript 使用href Jquery值更新输入
我的链接:Javascript 使用href Jquery值更新输入,javascript,jquery,Javascript,Jquery,我的链接: <a href="<?php echo $r['price']; ?>" id="buyorderprice" value=""><?php echo $r['price']; ?></a> 价格值只是一个有效的数字。此外,我不希望在单击按钮时重新加载页面,只需更新sellprice输入,即: <input type="text" class="form-control" id="sellprice" name="sellpri
<a href="<?php echo $r['price']; ?>" id="buyorderprice" value=""><?php echo $r['price']; ?></a>
价格值只是一个有效的数字。此外,我不希望在单击按钮时重新加载页面,只需更新sellprice输入,即:
<input type="text" class="form-control" id="sellprice" name="sellprice" value="<?php echo
$coinTicker->price($coin2[1] , 'sell'); ?>">
cointicker是随机数的默认值
$("#buyorderprice").click(function() {
$('#sellprice').val($('#buyorderprice').attr('href'));
});
。。。因为buyorderprice是id
要使其更具可操作性,请将href改为read href=并使用value
<a href="#" id="buyorderprice" data-value="<?php echo $r['price']; ?>"><?php echo $r['price']; ?></a>
使用javascript/Vanilla JS,您可以如下实现:
var a = document.getElementById('buyorderprice');
a.addEventListener('click',setValueNow);
function setValueNow(e) {
e.preventDefault();
var b = document.getElementById('sellprice').value = document.getElementById('buyorderprice').getAttribute('href');
}
输入的值被更改,但是页面被重新加载,您看不到更改了什么。因此,您需要在单击按钮时停止重新加载页面 下面是执行此操作的代码:
$("#buyorderprice").click(function(e) {
e.preventDefault();
$('#sellprice').val($('#buyorderprice').attr('href'));
});
做了一些修改,这将有助于,我想,S没有价值,你只能获取数据属性,我认为这不是好的实践,使用真实的数据属性来保持维护和可读性。等等。你应该解释你的答案是如何解决问题的,而不仅仅是给他写一些代码。另外,如果您使用e.preventDefault,您不需要返回false。实际上,我喜欢这个简单的答案,但是@Barmar是正确的,需要更多的描述,并且它保留了其他不好的实践问题。
var a = document.getElementById('buyorderprice');
a.addEventListener('click',setValueNow);
function setValueNow(e) {
e.preventDefault();
var b = document.getElementById('sellprice').value = document.getElementById('buyorderprice').getAttribute('href');
}
$("#buyorderprice").click(function(e) {
e.preventDefault();
$('#sellprice').val($('#buyorderprice').attr('href'));
});