javascript 为什么 new jQuery on('change') 在 iOS 上不触发,而在 Android 上工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9513268/
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
Why does new jQuery on('change') not fire on iOS, while it works on Android?
提问by frequent
I'm trying to update a piece of code to the new jQuery on()
method. I have a table and a select box. I want to populate the select box with one option for each table header cell, which works fine.
我正在尝试将一段代码更新为新的 jQueryon()
方法。我有一张桌子和一个选择框。我想用每个表格标题单元格的一个选项填充选择框,这很好用。
Then I want to toggle header cells depending on options selected. This works on desktop and Android but not on iPad.
然后我想根据选择的选项切换标题单元格。这适用于桌面和 Android,但不适用于 iPad。
This is what I'm doing:
这就是我正在做的:
$('table th').each(function(i) {
// add option to select box for each table column
var toggle = $('<option value="'+id+'">'+th.text()+'</option>');
$('select#toggle').append(toggle); }
// set up options
$('select#toggle').one("updateCheck", function(){
console.log("th-setup");
// if condition, then select, otherwise don't select
}).trigger("updateCheck");
});
// listen for select changes
$('select#toggle').on('change', function() {
console.log("change detected");
})
HTML
HTML
<select id="toggle"></select>
<table class="sample">
<thead>
<tr>
<th>one</th>
<th>two</th>
<th>three</th>
<th>four</th>
</tr>
</thead>
</table>
I first tried to bind the following code to document like so:
我首先尝试将以下代码绑定到文档,如下所示:
$(document).on('change',$('select#toggle'), function(...)
but this simply crashes my iPad before the page is displayed.
但这只会在页面显示之前使我的 iPad 崩溃。
Can someone tell me if I'm doing something wrong and why selecting an option does not trigger a change event on iPad vs triggering one on Android and desktop?
有人能告诉我我是否做错了什么,为什么选择一个选项不会在 iPad 上触发更改事件,而不是在 Android 和桌面上触发更改事件?
回答by Pointy
It should be:
它应该是:
$('body').on('change', '#toggle', function() { ... });
The first argument is the list of event names. The second is a selector string (not a jQuery object). The third is the handler function.
第一个参数是事件名称列表。第二个是选择器字符串(不是 jQuery 对象)。第三个是处理函数。
There's no need to qualify an identifier reference in a selector with the node type.
无需使用节点类型限定选择器中的标识符引用。