点击事件一次触发

时间:2016-12-20 09:19:31

标签: javascript jquery html5 dom

我在课程select2-search-choice-close上触发了点击事件,但我只能触发第一次/第一次出现的课程select2-search-choice-close

以下是我的HTML代码。

<ul class="select2-choices" style="">  
    <li class="select2-search-choice">    
        <div>Insurance Broker</div>    
        <a href="#" class="select2-search-choice-close" tabindex="-1"></a>
    </li>
    <li class="select2-search-choice">    
        <div>Insurance Broker</div>
        <a href="#" class="select2-search-choice-close" tabindex="-1"></a>        
    </li>
    <li class="select2-search-choice">    
        <div>Other business </div>    
        <a href="#" class="select2-search-choice-close" tabindex="-1"></a>
    </li>
    <li class="select2-search-field">
        <label for="s2id_autogen1" class="select2-offscreen"></label>
        <input autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input" id="s2id_autogen1" tabindex="0" style="width: 10px;" placeholder="" aria-activedescendant="select2-result-label-83" type="text">  
    </li>
</ul>

Jquery代码:

$( ".select2-search-choice" ).each(function( index ) {
    if($.trim($( this ).text()) != 'No, I am not in a business.') {
        $(".select2-search-choice-close").trigger("click");
    }
});  

我做错了什么?

提前致谢!

1 个答案:

答案 0 :(得分:2)

选择相对于循环元素的项目

YourTextbox.Text = line

演示:

Private Sub bReadFA_Click(sender As Object, e As EventArgs) Handles bReadFA.Click
For Each Line As String In File.ReadLines("C:\users\sk\16file.txt")
YourTextbox.Text = line
await Task.Delay(1000)//Await a second
Next
$( ".select2-search-choice" ).each(function( index ) {
        if($.trim($( this ).text()) != 'No, I am not in a business.') {
            $(this).find(".select2-search-choice-close").trigger("click");
        }
});