使用 javascript/jQuery 将随机数生成到文本输入中

Generate random number into text input using javascript/jQuery

本文关键字:文本 输入 随机数 javascript jQuery 使用      更新时间:2024-04-23

我正在尝试在文本字段中生成一个随机数。不幸的是,ID 在这里不是一个选项,所以我使用类作为标识符。我已经尝试了很多东西,但似乎无法完成这项工作。

用于输入的 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;
});

尝试了各种其他功能和组合,但无济于事。

带有类 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>

有效:http://jsfiddle.net/3hhCx/

$('.productAttributeConfigurableEntryNumbersOnlyText.productAttributeValue')

正在选择同时具有 .productAttributeConfigurableEntryNumbersOnlyText .productAttributeValue的元素。

您要选择一个具有.productAttributeValue的元素,该元素是.productAttributeConfigurableEntryNumbersOnlyText的子元素。

为此,请在它们之间添加一个空格:

$('.productAttributeConfigurableEntryNumbersOnlyText .productAttributeValue')

要插入随机值,请使用:

var randomnumber=Math.floor(Math.random()*11)    
$('.productAttributeConfigurableEntryNumbersOnlyText .productAttributeValue input[type="text"]').val( randomnumber ); 

试试这个....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:

var randomnumber=Math.floor(Math.random()*11)    
$('#BuyThis').click(function(){    
  $(".productAttributeConfigurableEntryNumbersOnlyText .productAttributeValue input:text").val( randomnumber ); 
});

尝试演示