如何使用jQuery在加载事件上动态更改占位符的值?



我正在从事的项目,每次用户刷新页面时,我都想更改占位符。我只想在占位符中排名第 1 到 9 位。

<div class="control-group">
    <div class="controls">
        <div class="main_input_box">
            <label >PIN </label>
            <input class="pass" type="password" placeholder="5th" />
            <input class="pass" type="password" placeholder="2nd" />
            <input class="pass" type="password" placeholder="1st" />
        </div>
    </div>
</div>

当用户加载页面时,所有占位符的值都应该改变,类似于 PHP 中的 rand() 函数。如何在jQuery中做到这一点?

我希望它能帮助你:

var temp;
var nums = [];
$(document).ready(function () {
    $('.pass').each(function(i, obj) {
        do {
            temp = Math.floor(Math.random() * 9) + 1;
            $(this).attr("placeholder", temp);
        } while (nums.indexOf(temp) != -1);
        nums.push(temp);
    });
});

最大 9 和 1 英寸

最新更新