开发者

JQuery: Insert a random string of 55 chars into a text box

开发者 https://www.devze.com 2023-02-28 15:56 出处:网络
I have a text input field, which on DOM ready, I want to have pre-populated with some random text, I dont want to have the same text each time I reload the page, just a slight variation between the te

I have a text input field, which on DOM ready, I want to have pre-populated with some random text, I dont want to have the same text each time I reload the page, just a slight variation between the text would be fine.

Here is the JQuery Field:

// on dom ready
    $("#Title").val("This is the Title");
// end dom ready

I would like the above field to be populated with some random text, MAX: 55 Characters, I was using the following script from Dynamic Drive, This works for web page content, but not for what I want to do with JQuery.

function filltext(words){
var gibberish=["This is just some filler text", "Some Random Text开发者_如何学编程 Here", "Even more Random Text"]
for (var i=0; i<words; i++)
document.write(gibberish[Math.floor(Math.random()*3)]+" ")
}


So basically, I want a input field to be pre-populated on DOM Ready, with some random text (That I could specify). BUT cannot exceed 55 Characters.


Surely, just replace document.write with $('#Title').val.

var gibberish = [
   "This is just some filler text",
   "Some Random Text Here",
   "Even more Random Text"
];

$(function() {
   $('#Title').val(gibberish[Math.floor(Math.random()*3)]+" ");
});


I'm assuming you just want one of the sentences from gibberish that is under a certain length. I made a small jQuery plugin for selecting and assigning a value at random based on some options.

jQuery.valRoulette

(function( $ ){
    $.fn.valRoulette = function( source, options ){ 
        options = $.extend({'max' : 99999999, 'min': 0}, options);
        var items = $(source).filter(function(){
            return options.max > this.length &&
                   options.min < this.length;});
        var index = Math.floor(Math.random()*items.length);
        return this.val( items[ index ]);
    };
})(jQuery);

jQuery.valRoulette( source, [ options ] )

source An array of strings.

options A set key/value pairs indicating what the properties of the text should be.

Options

max - Max length of the string to set.

min - Min length of the string to set.

Example

var gibberish=["foo",
    "bar",
    "John Skeet",
    "Stackoverflow",
    "Hire me: kitsunde@gmail.com"];

$("input").valRoulette( gibberish, {'max': 8 } );

Working version on JsLint.


Did you try this ??

you can probably include a large text in the array

http://leo.dolcepixels.com/learning/jquery/random-text-from-array-with-jquery/


Not sure what words is in your example, but from what I'm understanding you can easily use the same technique with your jquery.

var gibberish=["This is just some filler text", "Some Random Text Here", "Even more Random Text"];

$(function(){
   $("#Title").val(gibberish[Math.floor(Math.random()*gibberish.length)]);
})

EDIT: If you want to truncate the strings to a certain length, try this:

var gibberish=["This is just some filler text", "Some Random Text Here", "Even more Random Text"];

function setRandomText(charLength){
    $("#Title").val(gibberish[Math.floor(Math.random()*gibberish.length)].substring(0,charLength));
}

$(function(){
   setRandomText(9);
})

Some notes on this though. If you pass a value which is very large and it grabs a string which is smaller than the value passed it will simply return the whole string. If the contents of the string don't need to have any meaning, you may want to instead just create a big list of random words and have it patch together enough words to be as long as you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号