如何在文件准备好的情况下点击事件...?

时间:2022-12-03 22:00:24

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

如何在文档准备好的情况下触发qololbl click事件。这段代码不起作用为什么..?提前致谢。帮我

2 个解决方案

#1


8  

Use .click() instead of .trigger(). And put it after event hanlder declaration.

使用.click()而不是.trigger()。并在事件处理者声明之后提出。

$(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)

替代方案(由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>

#2


2  

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.

代码逐行执行。在将事件应用于元素之前触发click事件。这就是没有事件被触发并且没有启动警报的原因。

See Leguest answer for correct ordering.

有关正确的订购,请参阅Leguest答案。

#1


8  

Use .click() instead of .trigger(). And put it after event hanlder declaration.

使用.click()而不是.trigger()。并在事件处理者声明之后提出。

$(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)

替代方案(由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>

#2


2  

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.

代码逐行执行。在将事件应用于元素之前触发click事件。这就是没有事件被触发并且没有启动警报的原因。

See Leguest answer for correct ordering.

有关正确的订购,请参阅Leguest答案。