我需要在两种情况下运行一个函数。一种是在页面加载时运行,另一种是每次单击元素时运行。click 事件起作用,但 onload 事件不起作用。
$(document).ready(function(e) {
var captcha;
$('#captcha').on('ready click',function(){
function alphanumeric_unique() {
return Math.random().toString(36).split('').filter( function(value, index, self) {
return self.indexOf(value) === index;
}).join('').substr(2,8);
}
captcha = alphanumeric_unique();
$(this).val(captcha);
});
});
<input type="text" readonly value="" id="captcha">
请问有什么帮助吗?
最佳答案
您需要在页面加载时触发点击事件:
$(document).ready(function(e) {
$('#captcha').on('click',function(){
function alphanumeric_unique() {
return Math.random().toString(36).split('').filter( function(value, index, self) {
return self.indexOf(value) === index;
}).join('').substr(2,8);
}
captcha = alphanumeric_unique();
$(this).val(captcha);
});
$('#captcha').trigger("click"); // it will automatically call click event
});
关于javascript - 在多个事件上运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37593826/