Internet Explorer игнорирует команды javascript onblur () и focus () - PullRequest
1 голос
/ 06 января 2011

У меня следующая проблема. У меня есть страница с несколькими формами. Мне нужно изменить настройки tabindex по умолчанию для некоторых форм. Если я использую HTML-атрибут tabindex, то он нарушает «рабочие» формы, поэтому я использую JQuery для перехвата событий blur () и установки focus () для правильных элементов ввода.

Мой HTML:

<table>
            <tr>
                <td>Property Name: </td>
                <td><input type="text" class="Text" id="property_name" name="property_name" /></td>
                <td width="50">&nbsp;</td>
                <td>Site Contact: </td>
                <td valign="top" rowspan="3">
                    <textarea class="Default" id="site_contact" name="site_contact"></textarea>
                </td>
            </tr>
            <tr>
                <td>Address: </td>
                <td>
                    <input type="text" class="Text" id="property_address" name="property_address" />
                </td>
            </tr>
            <tr>
                <td>City: </td>
                <td>
                    <input type="text" class="ShortText" id="property_city" name="property_city" />
                </td>
            </tr>
            <tr>
                <td>State: </td>
                <td>
                    <input type="text" class="ShortText" id="property_state" name="property_state" />
                </td>
                <td width="50">&nbsp;</td>
                <td>Comments: </td>
                <td valign="top" rowspan="3">
                    <textarea class="Default" id="property_comments" name="property_comments"></textarea>
                </td>
            </tr>
            <tr>
                <td>Zip: </td>
                <td>
                    <input type="text" class="ShortText" id="property_zip" name="property_zip" />
                </td>
            </tr>
            <tr>
                <td>Description: </td>
                <td><?php Utilities::showPropertyDescriptionDdl('property_description', 'property_description'); ?></td>
            </tr>
            <tr><td>&nbsp;</td></tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    <input type="submit" class="submit" value="Save" id="SaveProperty" /> &nbsp;
                    <input type="button" class="simplemodal-close" value="Cancel" id="CancelProperty" />
                </td>
            </tr>
        </table>

Мой JQuery:

$('#PropertyForm [name=property_name]').blur( function() { $('#PropertyForm [name=property_address]').focus(); });
$('#PropertyForm [name=property_address]').blur( function() { $('#PropertyForm [name=property_city]').focus(); });
$('#PropertyForm [name=property_city]').blur( function() { $('#PropertyForm [name=property_state]').focus(); });
$('#PropertyForm [name=property_state]').blur( function() { $('#PropertyForm [name=property_zip]').focus(); });
$('#PropertyForm [name=property_zip]').blur( function() { $('#PropertyForm [name=property_description]').focus(); });
$('#PropertyForm [name=property_description]').blur( function() { $('#PropertyForm [name=site_contact]').focus(); });
$('#PropertyForm [name=site_contact]').blur( function() { $('#PropertyForm [name=property_comments]').focus(); });

Что должно происходить : Когда элемент property_name теряет фокус, элемент property_address должен получить.

Что происходит: Когда элемент property_name теряет фокус, элемент site_contact получает фокус, а затем сразу же перенаправляет фокус на элемент property_comments.

Это происходит в Internet Explorer 7 и 8. В FireFox все работает как положено. Есть ли что-то о наличии двух элементов «в ряд», которым назначены события onblur, то есть property_name и site_contact происходят последовательно в HTML.

Ответы [ 3 ]

2 голосов
/ 06 января 2011

Вы должны попробовать .focusout () и .focusin ().Большая разница, как отмечено в API, заключается в том, что они всплывают, но, если я правильно помню, они также работают в IE6 + jQuery API - focus Out

0 голосов
/ 06 января 2011

Я бы использовал событие нажатия клавиш, чтобы перехватить нажатие клавиши табуляции.

$('#PropertyForm [name=property_name]').keydown( 
    function(e) { 
        if (!e.shiftKey && e.keyCode == 9) {
            $('#PropertyForm [name=property_address]').focus();
            e.preventDefault();
        }
});
$('#PropertyForm [name=property_address]').keydown( 
    function(e) { 
        if (!e.shiftKey && e.keyCode == 9) {
            $('#PropertyForm [name=property_city]').focus();
            e.preventDefault();
        }
});
0 голосов
/ 06 января 2011

Я думаю, что вы хотите отключить функцию JavaScript по умолчанию.Который будет выполняться после вашего кода.

попробуйте что-нибудь подобное.Это всего лишь псевдокод, но я думаю, что вам нужно сообщить javascript, что вы не хотите, чтобы он выполнял встроенные обработчики событий по умолчанию.

.blur(function() { //your code here; return false});
...