2011-08-19 9 views
0

各グループの各選択/オプション値を識別(アラート)します。しかし、私はプッシュを必要とする ...jqueryセレクタ - 各グループの各選択/オプション値を特定します

<script src="jquery.mobile/jquery.js"></script> 

<div id="groupA" class="preGroups"> 

    <div id="section-A1"> 
    <input name="SRPR1" type="text"> 
     <select name='ACTC' id='preAction' > 
      <option value='007'>Stolen</option> 
      <option value='008'>Tampered</option> 
     </select> 
    </div> 

    <div id="section-A2"> 
    <input name="SRPR1" type="text"> 
     <select name='ACTC' id='preAction' > 
      <option value='007'>Stolen</option> 
      <option value='008'>Tampered</option> 
     </select> 
    </div> 

</div> 

<div id="groupB" class="preGroups"> 

    <div id="section-B1"> 
    <input name="SRPR1" type="text"> 
     <select name='ACTC' id='preAction' > 
      <option value='007'>Stolen</option> 
      <option value='008'>Tampered</option> 
     </select> 
    </div> 

    <div id="section-B2"> 
    <input name="SRPR1" type="text"> 
     <select name='ACTC' id='preAction' > 
      <option value='007'>Stolen</option> 
      <option value='008'>Tampered</option> 
     </select> 
    </div> 

    <script> 
$(document).ready(function() 
{  
     // iterate through each group in groups 
     groups = $('div[id^="group"]'); 
     $.each(groups, function(key, group) { 
      fnValidateGroup($(group));  
     }); 

     // validation for reason codes in a specific Group 
     function fnValidateGroup(currentGroup){ 

      selects = $(currentGroup).find('select[name="ACTC"]'); 
      $.each(selects, function(key, activity) { 
       // show me activity seelctec in each case 
       alert($(activity).val) ; 
      }); 
     } 
});  



</script> 

* 編集* 出力は次のようになります。

new Group 
007 
008 
new Group 
007 
007 

* 編集ここ* 私はあなたの助けを借りに来た答えがあります...

$(document).ready(function() 
{  
     // iterate through each group in groups 
     groups = $('div[id^="group"]'); 
     $.each(groups, function() { 
      console.log("New Group"); 
      fnValidateGroup(this);  
     }); 

     // validation for reason codes in a specific Group 
     function fnValidateGroup(currentGroup){ 

      selects = $(currentGroup).find('select[name="ACTC"]'); 
      $.each(selects, function(key, activity) { 
       // show me activity seelctec in each case 
       console.log(($(activity)).val()); 
      }); 
     } 
});  
+0

あなたは違いがあることに気付くかもしれませんjQueryの各機能を試してみると、2つあります。 [this one](http://api.jquery.com/each/)はjQueryオブジェクトを反復処理します。 [そしてこれ(http://api.jquery.com/jQuery.each/)は、JavaScriptオブジェクトを反復処理します。あなたの目的のために、最初のものはjQueryオブジェクトを持っているので使いやすいです。次に、キー値変数を参照する必要はありません。私はあなたに私の答えで最初のものを使用していることがわかります。 – BZink

答えて

2

このようなものは、 ork。何を試しましたか?

$("select").each(function(){ 
    alert($(this).attr("name")); 
    $(this).children("option").each(function(){ 
     alert($(this).html()); 

    }); 
}); 

それとも、ペアでそれらを警告したい場合は(あなたの質問に基づいて確認されませんでした)

$("select").each(function(){ 
     var selectName = $(this).attr("name"); 
     $(this).children("option").each(function(){ 
      alert(selectName + " " + $(this).html()); 

     }); 
    }); 

ここではあなたのグループの検証と

var groups = $("div[id^='group']"); 
groups.each(function(){ 
    var thisGroup = $(this).attr("id"); 
    $(this).find("option").each(function(){ 
     alert(thisGroup + " " + $(this).val()); 

    }); 


}); 
+0

は解決策のように見えますが、私はグループ別の検証を追加したいと思います。 –

+0

グループによる検証はどういう意味ですか? – BZink

+0

のhtmlでは、データを「グループ」に分割し、グループごとに1つのIDを付けます。 –

関連する問題