我試圖在文本字段中生成一個隨機數。不幸的是ID在這裏不是一個選項,所以我使用類作爲標識符。我嘗試了很多東西,但似乎無法得到這項工作。使用javascript/jQuery將文本輸入生成隨機數
HTML輸入:
<div class="productAttributeRow productAttributeConfigurableEntryNumbersOnlyText">
<div class="productAttributeLabel">
<label>
<span class="name">
Hidden:
</span>
</label>
</div>
<div class="productAttributeValue">
<input type="text" class="Field validation" value="" size="5" />
</div>
</div>
我曾嘗試:
var randomnumber = Math.floor(Math.random() * 11)
$('#BuyThis').click(function() {
$(".productAttributeConfigurableEntryNumbersOnlyText.productAttributeValue input[type="
text "]").val(randomnumber);
});
在上述情況下,#BuyThis
一個按鈕:
<a id="BuyThis" type="button" class="btn btn-small btn-warning" href="#product-options" data-toggle="modal">
然而,這種情況發生,沒有必要點擊一下,我只需要在表單提交之前在該字段中輸入一個隨機數字。
也試過沒有點擊:
$(function(){
var randomnumber=Math.floor(Math.random()*11)
$('.productAttributeConfigurableEntryNumbersOnlyText.productAttributeValue input[type="text"]').val(randomnumber);
})
的另一種嘗試:
function randomNumber(m, n) {
m = parseInt(m);
n = parseInt(n);
randomnumber = Math.floor(Math.random() * (n - m + 1)) + m;
('.productAttributeConfigurableEntryNumbersOnlyText.productAttributeValue input[type="text"]').value = randomnumber;
});
嘗試各種其他功能和組合都無濟於事。
你可以提供您的問題的演示。? –