使用 jquery 以编程方式单击按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19732664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically click a button using jquery
提问by Jeff Shain
I have this button:
我有这个按钮:
<div class="btn-group bootstrap-select show-tick span12 reg-form-control validate[required]">
<button id="control-qid13228" type="button" class="btn dropdown-toggle btn-bootstrap" data-toggle="dropdown" data-id="qid13228">
<div class="filter-option pull-left">3 of 4 selected</div> <div class="caret"></div>
</button>
<div class="dropdown-menu open" style="max-height: 249px; overflow: hidden; min-height: 92px;">
<ul class="dropdown-menu inner" role="menu" style="max-height: 237px; overflow-y: auto; min-height: 80px;">
<li rel="0" class="selected"><a tabindex="0" onclick="update_reg_multi_click('qid13228','basketball');" style=""><span class="text">basketball</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
<li rel="1"><a tabindex="0" onclick="update_reg_multi_click('qid13228','baseball');" style=""><span class="text">baseball</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
<li rel="2" class="selected"><a tabindex="0" onclick="update_reg_multi_click('qid13228','football');" style=""><span class="text">football</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
<li rel="3" class="selected"><a tabindex="0" onclick="update_reg_multi_click('qid13228','hockey');" style=""><span class="text">hockey</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
</ul>
</div>
I've tried to click it programmatically using JQUERY
我尝试使用 JQUERY 以编程方式单击它
<script>
$(document).ready(function(){
$("#control-qid13228").click();
});
</script>
I've also tried a trigger click:
我也试过触发点击:
<script>
$(document).ready(function(){
$("#control-qid13228").trigger("click");
});
</script>
Neither seem to work. Clicking with my mouse, of course, works fine. Any ideas?
两者似乎都不起作用。当然,用我的鼠标单击工作正常。有任何想法吗?
Just a heads up, this is part of an attempt to address a different issue I'm having with bootstrap-select, addressed in this script in progress.
提醒一下,这是尝试解决我在 bootstrap-select 中遇到的另一个问题的一部分,该问题已在此脚本中解决。
function update_reg_multi_click(id,myvalue)
{
//
var main_id = $("#" + id);
var main_id_control = $("#control-" + id);
var main_id_option = $("#" + id + " " + ">" + " " + "option");
var main_id_value = $(main_id).val();
var this_div = $(this);
if(main_id_value != null){
var main_id_value_list = $("#" + id).val().toString();
var test_if_in_list=main_id_value_list.toLowerCase().indexOf(myvalue) >= 0;
if(test_if_in_list){
//alert('in list');
$(main_id_option).each(function() {
$(this).removeAttr("selected");
});
var newlist = remove_reg_val(main_id_value_list,myvalue,",");
$.each(newlist.split(','), function(){
var option_to_set = $("#" + id + " " + "option[value="+this+"]");
$(option_to_set).attr('selected','selected');
});
$(main_id).selectpicker('refresh');
}
else{
//alert('not in list');
$(main_id_option).each(function() {
$(this).removeAttr("selected");
});
//alert(main_id_value_list);
var newlist = main_id_value_list + ","+myvalue;
//alert(newlist);
$.each(newlist.split(','), function(){
var option_to_set = $("#" + id + " " + "option[value="+this+"]");
$(option_to_set).attr('selected','selected');
});
$(main_id).selectpicker('refresh');
}
}
else{
var newlist = myvalue;
$.each(newlist.split(','), function(){
var option_to_set = $("#" + id + " " + "option[value="+this+"]");
$(option_to_set).attr('selected','selected');
});
$(main_id).selectpicker('refresh');
}
}
Here is the select I'm trying to address:
这是我要解决的选择:
<select name="qid13228" id="qid13228" class="selectpicker span12 reg-form-control validate[required]" data-style="btn-bootstrap" multiple="" data-selected-text-format="count>2" style="display: none;">
<option value="basketball" onClick="update_reg_multi_click('qid13228','basketball');" selected="selected">basketball</option>
<option value="baseball" onClick="update_reg_multi_click('qid13228','baseball');">baseball</option>
<option value="football" onClick="update_reg_multi_click('qid13228','football');">football</option>
<option value="hockey" onClick="update_reg_multi_click('qid13228','hockey');" selected="selected">hockey</option>
</select>
回答by Alvaro
Add a small timeout:
添加一个小的超时:
$(document).ready(function(){
setTimeout(function(){
$("#control-qid13228").click();
},1);
});
https://jsfiddle.net/46my242x/1/
https://jsfiddle.net/46my242x/1/
or try:
或尝试:
$("#control-qid13228")[0].click();
https://jsfiddle.net/46my242x/2/
https://jsfiddle.net/46my242x/2/
This is a similar question: jQuery.trigger('click') not working
这是一个类似的问题:jQuery.trigger('click') not working
回答by Norbert Pisz
Let's try:
咱们试试吧:
<script>
$(document).ready(function(){
$("#control-qid13228").click(function(){
alert('clicked');
});
});
</script>