1

キープレスイベントを、動的に構築される要素のフォームにバインドする必要があります。以下を参照してください

<div class="Customer">
<select name="dropdown1" id="dropdown1">
</select>
<input type="textbox" name="firstname1" id="firstname1">
<input type="textbox" name="lastname1" id="lastname1">
</div>

<div class="Customer">
<select name="dropdown2" id="dropdown2">
</select>
<input type="textbox" name="firstname2" id="firstname2">
<input type="textbox" name="lastname2" id="lastname2">
</div>

... and repeat..

上記の要素のいずれかが変更されるたびに、他の要素が空でないことを確認する必要があります。

フォームには他にもいくつかの要素がありますが、コード セグメントで強調表示されている要素だけを気にします。

どうもありがとう、

次のコードを試しました

$('.Customer').each(function (index) {

    alert("test");  // alerts the correct number of customer rows

    $("input:textbox").click(function() {
        alert("");
    })

});

ソリューションはjQuery 1.3.2で動作するはずです

4

4 に答える 4

1

ここで動作するのを見てください

<input type="checkbox" id="test" name="test" disabled="disabled">

<div class="Customer">
<select name="dropdown1" id="dropdown1">
    <option value="1.1">1.1</option>
    <option value="1.2">1.2</option>
</select>
<input type="text" name="firstname1" id="firstname1">
<input type="text" name="lastname1" id="lastname1">
</div>

<div class="Customer">
<select name="dropdown2" id="dropdown2">
    <option value="2.1">2.1</option>
    <option value="2.2">2.2</option>
</select>
<input type="text" name="firstname2" id="firstname2">
<input type="text" name="lastname2" id="lastname2">
</div>​

$('.Customer input, .Customer select').bind("change keyup", function(){
     var me = $(this);
     var clicked = me.attr("name");
     var empty = false;
     $('.Customer input,select').each(function(){
         var elem = $(this);
             if (elem.val() == "") {
                 empty = true;
             }
     });
     if (empty) {
        $('#test').attr('disabled','disabled')
     } else {
        $('#test').removeAttr('disabled')
     }
});​

PS:入力にタイプtextbosはありません。それはテキストです。何かが変更されたときにコールバックを起動するには、「クリック」イベントではなく「変更」を使用します。ドロップダウンと入力の両方で機能させるには、「入力、選択」が必要です。

于 2012-06-07T19:35:40.307 に答える
1

これらのテキスト入力のそれぞれにイベントハンドラーをアタッチする場合は、次のようにする必要があります。

HTML(タイプは「テキストボックス」ではなく「テキスト」です)

<div class="Customer">
<select name="dropdown1" id="dropdown1">
</select>
<input type="text" name="firstname1" id="firstname1">
<input type="text" name="lastname1" id="lastname1">
</div>

<div class="Customer">
<select name="dropdown2" id="dropdown2">
</select>
<input type="text" name="firstname2" id="firstname2">
<input type="text" name="lastname2" id="lastname2">
</div>

js

$('.Customer').each(function (index) {
        $("input:text", this).click(function() {
        alert("");
    })

});​

http://jsfiddle.net/pSFj3/

編集-私はあなたが間違った構文を使用していることを指摘したかったのですが、多くの人がイベントを処理するためのより良い方法があることを指摘しています。たとえば、イベントの委任を使用できます

$('.Customer').on( 'click', 'input:text', function() {
   alert('clicked on input');
});
于 2012-06-07T19:36:00.650 に答える
1

クラス「Customer」の要素内のすべてのテキストボックスにクリックイベントを添付する場合は、次のように機能しないでください。

$(".Customer input[type=text]").click(function() { });
于 2012-06-07T19:36:39.493 に答える
0

フォームがクライアント上で動的に構築されている場合は、関数を使用する必要があり$.on()ます。

$(document).on('keypress', '.Customer input[type="text"]', function() { ... }); 
于 2012-06-07T19:38:23.583 に答える