1

顧客情報を含む 2 つのドロップダウン リストがあります。PHP for loop を使用して、一度に 5 人の顧客の詳細を入力できるようにしました。

for($i=1; $i<6; $i++)
{
   echo "<tr><td><select id=\"customer_" . $i . "_class\"></select></td>";
   echo "<td><select id=\"customer_" . $i . "_age\"></select></td></tr>";
}

ここで、たとえば customer_2_class が変更された場合に、対応する customer_2_age に対して何らかの関数を実行するように jQuery 関数を実行したいと考えています。

$("#customer_?_clas").change(function()
{
   //some function to execute on corresponding customer_?_age
});
4

3 に答える 3

3

クラスをあなたに追加し<select>、IDを次のような別の属性に移動しdataます。

echo "<tr>";
echo "<td><select class=\"customer_class\" data-id=\"$i\"></select></td>";
echo "<td><select class=\"customer_age\" data-id=\"$i\"></select></td>";
echo "</tr>";

そしてあなたのJavaScriptで:

$('.customer_class').change(function() {
  var id = $(this).attr('data-id');
  // process customer_class with this id
});
于 2012-06-01T02:55:26.230 に答える
0

属性 end-with セレクターを使用できます。

$('select[id$="_class"]').change(function() {

jQuery セレクターの完全なリストを参照する価値があるかもしれません。

于 2012-06-01T02:57:05.560 に答える
0
$('select').
   filter(function() { 
       return /^customer_[0-9]_class$/.test(this.id); // filter on select 
                                                      // based on id format
   }) 
   . change(function() {                              // binding change 
                                                      // to filtered select
       var num = this.id.match(/\d/)[0];              // get numerical value 
                                                      // of select box on change

       $('#customer_' + num + '_age')                 // point to target select 
            .css('background', '#f00');               // do something
   });

作業サンプル

于 2012-06-01T03:09:03.277 に答える