سؤال

I am using jQuery to select and pop an alert when user click button. The below code works on 'standard' browsers (Chrome, Firefox, etc), but IE 6 fails.

$('input[type="submit"]').click(function(event) {event.preventDefault();});
$('input[type="submit"]').click(function() {alert('hi')});

Looks like jQuery selector of such input won't work in IE 6. How can I make it compatible for IE 6 as well?

هل كانت مفيدة؟

المحلول

You can assign your buttons a class and then reference with that.

<input type="submit" class="runthis" />

$(".runthis").click(function() { });

EDIT: this code works for me in IE6:

<!doctype html>
<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {

            $("input[type='submit']").click(function() {

                alert("HI");
            });
        });
    </script>
</head>
<body>
    <form>
        <input type="submit" />
    </form>
</body>
</html>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top