[英]Generate random number into text input using javascript/jQuery
I'm trying to generate a random number into a text field.我正在尝试在文本字段中生成一个随机数。 Unfortunately ID's are not an option here so I'm using classes as identifiers.不幸的是,ID 在这里不是一个选项,所以我使用类作为标识符。 I've tried a number of things but cannot seem to get this work.我已经尝试了很多东西,但似乎无法完成这项工作。
HTML for input:用于输入的 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>
I have tried:我努力了:
var randomnumber = Math.floor(Math.random() * 11)
$('#BuyThis').click(function () {
$(".productAttributeConfigurableEntryNumbersOnlyText.productAttributeValue input[type="
text "]").val(randomnumber);
});
In the above scenario, #BuyThis
is a button:在上面的场景中, #BuyThis
是一个按钮:
<a id="BuyThis" type="button" class="btn btn-small btn-warning" href="#product-options" data-toggle="modal">
However, it's not necessary this happen with a click, I just want a random number in the field before the form is submitted.但是,这没有必要通过单击发生,我只需要在提交表单之前在字段中输入一个随机数。
Also tried without the click:也试过不点击:
$(function(){
var randomnumber=Math.floor(Math.random()*11)
$('.productAttributeConfigurableEntryNumbersOnlyText.productAttributeValue input[type="text"]').val( randomnumber );
})
Another attempt:另一种尝试:
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;
});
Tried various other functions and combinations to no avail.尝试了各种其他功能和组合都无济于事。
The div with class productAttributeValue
is inside productAttributeConfigurableEntryNumbersOnlyText
, so when selecting you should add a space between them:具有productAttributeValue
类的 div 位于productAttributeConfigurableEntryNumbersOnlyText
,因此在选择时应在它们之间添加一个空格:
$(function() { var randomnumber = Math.floor(Math.random() * 11) $('.productAttributeConfigurableEntryNumbersOnlyText .productAttributeValue input[type="text"]').val(randomnumber); })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <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>
Does work: http://jsfiddle.net/3hhCx/工作: http : //jsfiddle.net/3hhCx/
$('.productAttributeConfigurableEntryNumbersOnlyText.productAttributeValue')
Is selecting an element which has both .productAttributeConfigurableEntryNumbersOnlyText
and .productAttributeValue
.是选择具有两个元素.productAttributeConfigurableEntryNumbersOnlyText
和.productAttributeValue
。
You want to select an element which has .productAttributeValue
which is a child of .productAttributeConfigurableEntryNumbersOnlyText
.你要选择具有一个元素.productAttributeValue
这是一个孩子.productAttributeConfigurableEntryNumbersOnlyText
。
To do that, add a space between them:为此,请在它们之间添加一个空格:
$('.productAttributeConfigurableEntryNumbersOnlyText .productAttributeValue')
To insert a random value, use:要插入随机值,请使用:
var randomnumber=Math.floor(Math.random()*11)
$('.productAttributeConfigurableEntryNumbersOnlyText .productAttributeValue input[type="text"]').val( randomnumber );
try this... HTML试试这个... 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>
javascript: javascript:
var randomnumber=Math.floor(Math.random()*11)
$('#BuyThis').click(function(){
$(".productAttributeConfigurableEntryNumbersOnlyText .productAttributeValue input:text").val( randomnumber );
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.