在Safari中没有触发JQuery

在Safari中没有触发JQuery

本文介绍了在Safari中没有触发JQuery Click事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户从选择框中选择一个选项时,我正在尝试做某事 - 尽可能简单吗?我正在使用JQuery 1.3.1注册一个带有选择框id的点击处理程序。一切都很好,直到我使用Chrome和Safari测试,发现它不起作用。

I'm trying to do something when a user selects an option from a select box - As simple as can be right? I'm using JQuery 1.3.1 to register a click handler with the id of the select box. Everything was fine until I tested using Chrome and Safari and found it didn't work.


  1. Firefox 3.05 - 是

  2. IE 7.0.5730.13 - 是

  3. IE6Eolas - 是

  4. Sarafi 3.2.1 - 否

  5. Chrome 1.0.154.43 - 否

  1. Firefox 3.05 - YES
  2. I.E 7.0.5730.13 - YES
  3. IE6Eolas - YES
  4. Sarafi 3.2.1 - NO
  5. Chrome 1.0.154.43 - NO

请参阅以下代码:

<html>
<head>
    <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
    <script language="javascript">
    $(document).ready(function(){
        $('#myoption').click(function() { alert('Select Dropdown was clicked: '+ $('#myoption').val()); });
    });
    </script>
</head>
<body>
    <select id="myoption">
    <option value="A">A</option>
    <option value="B">B</option>
    </select>
</body>
</html>

任何人都知道我应该为此工作做些什么?警报最终会被触发,但只有在双击选择框后?

Anyone know what I should be doing for this to work? The alert does eventually get triggered but only after double-clicking the select box?

推荐答案

我发现了我的问题。对于选择框,您需要为更改事件而不是单击事件注册处理程序。很奇怪Firefox和IE使用click事件。

I found my problem. For Select boxes, you need to register a handler for a "change" event rather than a "click" event. It's strange that Firefox and IE work with the click event.

总之,以下代码适用于所有浏览器:

To sum up, the following code works across all browsers:

<html>
<head>
    <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
    <script language="javascript">
    $(document).ready(function(){
        $('#myoption').change(function() { alert('Select Dropdown was clicked: '+ $('#myoption').val()); });
    });
    </script>
</head>
<body>
    <select id="myoption">
    <option value="A">A</option>
    <option value="B">B</option>
    </select>
</body>
</html>

这篇关于在Safari中没有触发JQuery Click事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 11:55