最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Generate random number into text input using javascriptjQuery - Stack Overflow

programmeradmin2浏览0评论

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. I've tried a number of things but cannot seem to get this work.

HTML for input:

<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:

<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 binations to no avail.

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. I've tried a number of things but cannot seem to get this work.

HTML for input:

<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:

<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 binations to no avail.

Share Improve this question edited Jun 1, 2015 at 17:49 L84 46.3k59 gold badges181 silver badges259 bronze badges asked Dec 8, 2013 at 13:54 susansusan 3822 gold badges6 silver badges19 bronze badges 1
  • can you provide a demo of your problem.? – Rajaprabhu Aravindasamy Commented Dec 8, 2013 at 13:56
Add a ment  | 

3 Answers 3

Reset to default 3

The div with class productAttributeValue is inside productAttributeConfigurableEntryNumbersOnlyText, so when selecting you should add a space between them:

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

})
<script src="https://cdnjs.cloudflare./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/3hhCx/

$('.productAttributeConfigurableEntryNumbersOnlyText.productAttributeValue')

Is selecting an element which has both .productAttributeConfigurableEntryNumbersOnlyText and .productAttributeValue.

You want to select an element which has .productAttributeValue which is a child of .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

<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 ); 
});

try Demo

发布评论

评论列表(0)

  1. 暂无评论