2012-02-29 10 views
0

Jquery change()を使用すると、フォームフィールドだけでなくフォーム1フィールドだけを選択するにはどうすればよいですか?以下は私のコードです。現在、両方のフォームのフィールドの検出が変更されていますが、フォーム1のフィールドの変更を検出するだけです。 Form1のからのみ変更イベントを検出するJQueryで複数のフォームからフォームを選択

<form id="form1" name="form1" method="post" action=""> 
<label for="textfield"></label> 
<input type="text" name="textfield" id="textfield" /> 
<label for="select"></label> 
<select name="select" id="select"> 
<option value="yes">yes</option> 
<option value="no">no</option> 
</select> 
</form> 

<form id="form2" name="form2" method="post" action=""> 
<label for="textfield2"></label> 
<input type="text" name="textfield2" id="textfield2" /> 
<label for="select2"></label> 
<select name="select2" id="select2"> 
<option value="yes">yes</option> 
<option value="no">no</option> 
</select> 
</form> 

<script> 
$(':input').change(function() { 
     alert('Handler for .change() called.'); 
     }); 
</script> 

答えて

1

$('#form1 :input').change(function() { 
    alert('Handler for .change() called.'); 
}); 

あなたは

$('#form2 :input').change(function() {... 
0

にそれを変更する必要があると思いますForm2のために同じことを行うにはあなたが特定のクラスを追加することができますあなたのフォーム1のものとそれを参照してください。

<form class="form1sel" id="form1" name="form1" method="post" action=""> 
<label for="textfield"></label> 
<input class="form1sel" type="text" name="textfield" id="textfield" /> 
<label for="select"></label> 
<select class="form1sel" name="select" id="select"> 
<option value="yes">yes</option> 
<option value="no">no</option> 
</select> 
</form> 

<script> 
$('.form1sel').change(function() { 
    alert('Handler for .change() called.'); 
}); 
</script> 

またはちょうど

<script> 
$('#form1 :input').change(function() { 
    alert('Handler for .change() called.'); 
}); 
</script> 
1

http://jsfiddle.net/escusado/nVxFh/

「変更」イベントは、あなたがコンテナレベルでそれをキャッチすることができます泡より具体的なセレクタを使用します。

$('#form1').change(function(){ 
    $('#console').text('te'); 
    //change actions 
}); 
関連する問題