7

私は現在、選択ボックスの値をチェックし、そのすぐ隣にあるテキスト フィールドを有効にして、できればフォーカスを設定するスクリプトを作成しました。

現時点では、入力フィールドを有効にするのに問題なく動作するこれがあります...

$("#assessed select").change(function () {

    if($(this).val() == 'null') { $(this).next('input').attr("disabled", true);  }
    else { $(this).next('input').removeAttr("disabled"); }

});

正直なところ、「フォーカス」ビットに少しこだわっています。「$(this).next('input').focus();」を試しました。Javascriptエラーも発生しませんでしたが、それはまったく焦点を合わせませんでした...

$("#assessed select").change(function () {

    if($(this).val() == 'null') { $(this).next('input').attr("disabled", true); $(this).next('input').focus(); }
    else { $(this).next('input').removeAttr("disabled"); }

});

アイデアはありますか?私は本当にこれにこだわっています。それは非常にシンプルですが、私が構築しているページへの非常に便利な追加です!

ありがとう

4

3 に答える 3

16

また、次の入力を取得するための素敵な小さなプラグインを見つけました:http: //jqueryminute.com/set-focus-to-the-next-input-field-with-jquery/

$.fn.focusNextInputField = function() {
    return this.each(function() {
        var fields = $(this).parents('form:eq(0),body').find('button,input,textarea,select').not(':hidden');
        var index = fields.index( this );
        if ( index > -1 && ( index + 1 ) < fields.length ) {
            fields.eq( index + 1 ).focus();
        }
        else {fields.first().focus();}
        return false;
    });
};
于 2011-01-05T08:44:49.257 に答える
5

このjqオブジェクトをキャッシュして、上記の回答を変更しました。また、next内のフィルターは必要ありません。next()は、次の兄弟のみを返します。フィルターは基本的に、それが入力またはあなたが与えるフィルターである場合にのみ、次を取得すると言っています。次が目的のオブジェクトであることが確実な場合は、フィルターを含める必要はありません。

$("#assessed select").change(function () {
    var $this = $(this);
    if( $this.val() === 'null') {
        $this.next()
             .attr("disabled", true);
    }
    else {
        $this.next()
             .removeAttr("disabled")
             .focus();
    }
});
于 2009-08-05T10:46:12.437 に答える
1

あなたの問題は、無効になっている入力コントロールにフォーカスを設定できないことだと思います(少なくとも一部のブラウザ/オペレーティングシステムでは)。

あなたの呼び出しは基本的に間違ったブロックにあります - それはブロックではなくブロックにfocus()あるべきです。elseif

このような:

$("#assessed select").change(function () {

    if($(this).val() == 'null') { $(this).next('input').attr("disabled", true); }
    else { $(this).next('input').removeAttr("disabled"); $(this).next('input').focus(); }
});
于 2009-08-05T10:34:34.690 に答える