gpt4 book ai didi

javascript - 在多个事件上运行函数

转载 作者:行者123 更新时间:2023-11-29 19:12:42 25 4
gpt4 key购买 nike

我需要在两种情况下运行一个函数。一种是在加载页面时,另一种是每次单击元素时。点击事件有效,但 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com