admin管理员组文章数量:1221421
Radio Button
<input name="radio-choice-t-6" class="qololbl" id="radio-choice-t-6a" value="qolo" checked="checked" type="radio">
<label for="radio-choice-t-6a" style="border: 1px solid #2d5f7c;width:90px;border-bottom-left-radius:4px;border-top-left-radius:4px;font-family:Noto Sans;">Toast</label>
$(document).ready(function (e) {
$('.qololbl').trigger('click');
$(".qololbl").click(function(){
alert("Hi");
});
});
how to fire qololbl click event on document ready. this code is not working why..? thanks in advance. help me
Radio Button
<input name="radio-choice-t-6" class="qololbl" id="radio-choice-t-6a" value="qolo" checked="checked" type="radio">
<label for="radio-choice-t-6a" style="border: 1px solid #2d5f7c;width:90px;border-bottom-left-radius:4px;border-top-left-radius:4px;font-family:Noto Sans;">Toast</label>
$(document).ready(function (e) {
$('.qololbl').trigger('click');
$(".qololbl").click(function(){
alert("Hi");
});
});
how to fire qololbl click event on document ready. this code is not working why..? thanks in advance. help me
Share Improve this question asked May 11, 2017 at 15:21 K-SeriesK-Series 1891 gold badge3 silver badges17 bronze badges 1 |2 Answers
Reset to default 13Use .click()
instead of .trigger()
. And put it after event hanlder declaration.
$(document).ready(function (e) {
$(".qololbl").click(function(){
alert("Hi");
});
$('.qololbl').click();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="radio-choice-t-6" class="qololbl" id="radio-choice-t-6a" value="qolo" checked="checked" type="radio">
<label for="radio-choice-t-6a" style="border: 1px solid #2d5f7c;width:90px;border-bottom-left-radius:4px;border-top-left-radius:4px;font-family:Noto Sans;">Toast</label>
Alternative (Suggested by pratik-gaikwad)
$(document).ready(function (e) {
$(".qololbl").on('click', function(){
alert("Hi");
});
$('.qololbl').trigger('click');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="radio-choice-t-6" class="qololbl" id="radio-choice-t-6a" value="qolo" checked="checked" type="radio">
<label for="radio-choice-t-6a" style="border: 1px solid #2d5f7c;width:90px;border-bottom-left-radius:4px;border-top-left-radius:4px;font-family:Noto Sans;">Toast</label>
The code executes line by line. You are triggering the click event before applying the event to the element. This is why no event is being fired and no alert is being launched.
See Leguest answer for correct ordering.
本文标签: javascripthow to fire click event on document readyStack Overflow
版权声明:本文标题:javascript - how to fire click event on document ready...? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739281857a2156281.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
click
event before you've set up aclick
event handler. – Scott Marcus Commented May 11, 2017 at 15:23