JQuery .focus()事件更改DropDown选项

时间:2011-04-06 13:38:48

标签: javascript jquery html focus telerik

我有一个selectlist,它是在网格中使用jquery构建的。当用户输入一个键构建选择列表时,我然后调用click()和focus(),以便我可以再次使用键命令。

我遇到的问题是.focus()在触发时会触发选择列表的选择。有没有人以前经历过这种行为或找到了解决方法?提前致谢

function INTEREST_createPaymentTypeDropDown() {
    //Interest specific:
    //build a dropdown for the enum
    //if the PaymentType input exists
    if ($("#PaymentType").length > 0) {

        var cellPaymentTypeValue = $("#PaymentType").val();
        var selectedOption;

        $("#PaymentType").replaceWith("<select id='PaymentType' style='width:95px; height: 20px;' name='PaymentType'></select>");
        $.each(INTEREST_PAYMENT_TYPES, function(key, val) {
            var newoption = $("<option value=\"" + val + "\">" + key + "</option>");

            if (val == cellPaymentTypeValue || key == cellPaymentTypeValue) {
                //append select to the current value
                selectedOption = val;
            }
            $("#PaymentType").append(newoption);
            $("#PaymentType").addClass("t-input");
        });

        if (selectedOption != null) {
            $("#PaymentType").val(selectedOption.toString());
        }
    }


    function INTEREST_setEditField(field) {
        if ($("#PaymentType").length > 0) {
            // this is where the problem is
            $("#PaymentType").click();
        }
        else {
            $(".t-grid-content").find('.t-input').click();
            $(".t-grid-content").find('.t-input').select();
        }

        INTEREST_persistPaymentTypeOnCellChange(field);
    }

    function INTEREST_persistPaymentTypeOnCellChange(field) {
        //keep the value of the enum not the enum index
        //in the payment type field
        var paymentTypeCell = $(field).parent().children().eq(1);

        if ($(paymentTypeCell).html().length == 1) {
            $.each(INTEREST_PAYMENT_TYPES, function(key, val) {
                if ($(paymentTypeCell).html() == val) {
                    $(paymentTypeCell).html(key);
                }
            });
        }
    }

    $('td.t-grid-edit-cell', 'div#AccountPayments').live('keydown', function(event) {
        if (event.which == 39 || event.which == 9) //Tab or right arrow
        {
            if ($(this).hasClass('t-last')) {
                INTEREST_goToNextRow(this, event);
            }
            else {
                INTEREST_goToRight(this, event);
            }
        }
        else if (event.which == 37 || event.which == 9 && event.shiftKey) //left arrow. Todo: add shift tab
        {
            if ($(this).hasClass('t-first')) {
                INTEREST_goToPreviousRow(this, event);
            }
            else {
                INTEREST_goToLeft(this, event);
            }
        }
        else if (event.which == 40) //down arrow
        {
            INTEREST_goDown(this, event);
        }
        else if (event.which == 38) //up arrow
        {
            INTEREST_goUp(this, event);
        }
    });

    $("#PaymentType").live('click', function(event) {
        $(this).focus();
    });

1 个答案:

答案 0 :(得分:0)

可能是列表顶部没有空选项,以便在调用click()时导致PaymentType自动选择第0个选项吗?