我試圖在div
中顯示更改值,但無法正常工作。這裏有一個例子:更改不起作用jQuery
button_up=document.getElementById('up');
button_down=document.getElementById('down');
button_up.onclick=function() {setQuantity('up');}
button_down.onclick=function() {setQuantity('down');}
quantity = document.getElementById('quantity');
function setQuantity(upordown) {
if (quantity.value > 1) {
if (upordown == 'up'){++quantity.value;}
else if (upordown == 'down'){--quantity.value;}}
else if (quantity.value == 1) {
if (upordown == 'up'){++quantity.value;}}
else
{quantity.value=1;}
}
$('.order-option input[type=\'text\']').change(function(){
$('.show').text($('#quantity').val())
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="order-option">Quantity: <span id="quantity-field">
<button id="down">-</button>
<input type="text" id="quantity" value="1">
<button id="up">+</button>
</span>
</div>
<div class="show"></div>
當你做一些事情會觸發一個改變事件時,它就可以正常工作。 *提示提示* –
你似乎正在混合jquery和jquery。嘗試保持某種一致性,我知道document.getElementById的作品,但既然你使用jQuery爲什麼不只是$(「#數量」)? – JonH