0

クラスや ID を持つ 2 つの親 div 内から入力テキスト ボックスを選択しようとしています。これを行うには非常に多くの例と方法があるため、他の誰かがこの問題に遭遇する可能性がある場合に備えて、この質問を投稿して支援することに頼りました.

コード

<div class="tblsearch">
    <div id="ElementCount10_filter" class="dataTables_filter">
        <label>
            Search:
            <input type="text">
        </label>
    </div>
    <div id="ElementCount10_info" class="dataTables_info"></div>
    <div class="clear"></div>
 </div>

私は次のことを試しました:

$(this).parent(".dataTables_filter").children("label").children("input").click(function () { alert('we got here'); });

$(".tblsearch.dataTables_filter label > input").click(function () {
    alert('hovered');
});

$(this).parent(".dataTables_filter").children("input").click(function () { alert('we got here'); });

セレクターにアクセスできませんでした。誰かがボックスをクリックして関数を起動したかどうかを確認する必要があります。これは非常に簡単に思えますが、構文に何かが欠けているに違いありません。

よろしくお願いします。

4

4 に答える 4

4

フィドル

$('.dataTables_filter').on('click', 'input', function() {
    alert('here we are!');
});

これを試して。より具体的にする必要がある場合はinput[type="text"]、何かを行うことができます。

これは、イベント委任を使用します (詳細については、@Methletics のリンクを参照してください)。inputしたがって、これは親内の任意の のクリックを処理します。

于 2013-08-13T19:46:52.417 に答える
1

2 番目のセレクターの問題は、クラス名の間にスペースがなかったことです。スペースを含めると、期待どおりに機能します。<div>with クラス.tblsearchは with クラスの祖先あるためです。<div>.dataTables_filter

$(".tblsearch .dataTables_filter label > input")

http://jsfiddle.net/MxffP/

于 2013-08-13T19:47:48.123 に答える
0

[デモ]はこちらです。

$(".dataTables_filter").children("label").children("input").click(function () { alert('we got here'); });

$(".dataTables_filter label > input").click(function () {
    alert('hovered');
});

$(".dataTables_filter").find("input").click(function () { alert('we got here'); });
于 2013-08-13T19:47:17.390 に答える