迭代块并获取输入元素值Jquery

时间:2018-04-15 00:03:46

标签: javascript jquery

我正在尝试迭代Qualtrics上的Block值。它包含以下结构:

    <div class="BlockOuter">
   <div id="block_43557007" class="Block Standard">
      <div class="BlockHeader">
         <span>CB_3</span>
         <div class="clear"></div>
      </div>
      <div class="QuestionsDiv">
         <div class="Question" id="QID12">
            <div class="SelectBorder">
               <div class="QuestionStructure">
                  <div class="QuestionInfoBar">
                     <div class="QuestionInfoBarInner">
                        <div class="QuestionNumber" style="font-size: 13px;">CB_3</div>
                     </div>
                  </div>
                  <div class="QuestionCanvas Skin OutlineMode">
                     <div>
                        <div class="QuestionText_Edit">
                           <div class="QuestionText" id="QuestionText_q33690646_EN" title="Who is Cesar Chavez?" ng-non-bindable="true">Who is Cesar Chavez?</div>
                        </div>
                        <div class="InfoBarButtons"><a class="Button " clickcallback="BaseQuestion.clearAll:q33690646($evt,true)">Clear</a></div>
                        <div class="InlineGrade">
                           <div class="choice-row">
                              <input class="form-control GradeInput GradePositive" size="1" value="1" questionid="q33690646" choiceid="1" validation="Number">
                              <span>
                                 <div class="SelectionContainer SelectionPositive" id="ch230074" clickcallback="BaseQuestion.toggleGrade" instanceid="q33690646" p1="1" p2="" p3="" title="Click to toggle scoring">
                                    <div class="ScoreSelection" clickcallback="BaseQuestion.toggleGrade" instanceid="q33690646" p1="1" p2="" p3="" title="Click to toggle scoring"><span class="ScoreSelectionText">Labor and farm worker advocate</span></div>
                                 </div>
                              </span>
                           </div>
                           <div class="choice-row">
                              <input class="form-control GradeInput" size="1" value="#" questionid="q33690646" choiceid="2" validation="Number">
                              <span>
                                 <div class="SelectionContainer SelectionNeutral" id="ch3336258" clickcallback="BaseQuestion.toggleGrade" instanceid="q33690646" p1="2" p2="" p3="" title="Click to toggle scoring">
                                    <div class="ScoreSelection" clickcallback="BaseQuestion.toggleGrade" instanceid="q33690646" p1="2" p2="" p3="" title="Click to toggle scoring"><span class="ScoreSelectionText">Salsa dancer</span></div>
                                 </div>
                              </span>
                           </div>
                           <div class="choice-row">
                              <input class="form-control GradeInput" size="1" value="#" questionid="q33690646" choiceid="3" validation="Number">
                              <span>
                                 <div class="SelectionContainer SelectionNeutral" id="ch92975033" clickcallback="BaseQuestion.toggleGrade" instanceid="q33690646" p1="3" p2="" p3="" title="Click to toggle scoring">
                                    <div class="ScoreSelection" clickcallback="BaseQuestion.toggleGrade" instanceid="q33690646" p1="3" p2="" p3="" title="Click to toggle scoring"><span class="ScoreSelectionText">Television personality</span></div>
                                 </div>
                              </span>
                           </div>
                           <div class="choice-row">
                              <input class="form-control GradeInput" size="1" value="#" questionid="q33690646" choiceid="4" validation="Number">
                              <span>
                                 <div class="SelectionContainer SelectionNeutral" id="ch7917009" clickcallback="BaseQuestion.toggleGrade" instanceid="q33690646" p1="4" p2="" p3="" title="Click to toggle scoring">
                                    <div class="ScoreSelection" clickcallback="BaseQuestion.toggleGrade" instanceid="q33690646" p1="4" p2="" p3="" title="Click to toggle scoring"><span class="ScoreSelectionText">Former Mexican president</span></div>
                                 </div>
                              </span>
                           </div>
                        </div>
                     </div>
                  </div>
                  <div class="clear"></div>
               </div>
            </div>
         </div>
         <div class="Question" id="QID13">
            <div class="SelectBorder">
               <div class="QuestionStructure">
                  <div class="QuestionInfoBar">
                     <div class="QuestionInfoBarInner">
                        <div class="QuestionNumber" style="font-size: 11px;">CB_3_con</div>
                     </div>
                  </div>
                  <div class="QuestionCanvas Skin OutlineMode">
                     <div>
                        <div class="QuestionText_Edit">
                           <div class="QuestionText" id="QuestionText_q76664205_EN" title="Please indicate how confident you are in the above answer" ng-non-bindable="true">Please indicate how confident you are in the above answer</div>
                        </div>
                        <div class="InfoBarButtons"><a title="Hold alt key to reverse code, plz" class="Button " clickcallback="BaseQuestion.scoreAll:q76664205($evt,null,true)">Auto</a></div>
                        <div class="InlineGrade">
                           <div class="choice-row">
                              <input class="form-control GradeInput" size="1" value="#" questionid="q76664205" choiceid="1" validation="Number">
                              <span>
                                 <div class="SelectionContainer SelectionNeutral" id="ch74193948" clickcallback="BaseQuestion.toggleGrade" instanceid="q76664205" p1="1" p2="" p3="" title="Click to toggle scoring">
                                    <div class="ScoreSelection" clickcallback="BaseQuestion.toggleGrade" instanceid="q76664205" p1="1" p2="" p3="" title="Click to toggle scoring"><span class="ScoreSelectionText">My confidence</span></div>
                                 </div>
                              </span>
                           </div>
                        </div>
                     </div>
                  </div>
                  <div class="clear"></div>
               </div>
            </div>
         </div>
      </div>
   </div>
   <div class="BlockShadow"></div>
</div>

这是块的外观图像:

enter image description here

这是jsfiddle的链接(虽然没有CSS) - https://jsfiddle.net/0LLabezt/

我正在尝试执行以下操作:

  1. 迭代所有&#34; BlockOuter&#34;的div
  2. 抓取QuestionNumber类的值(在本例中为CB_3,并存储在var = question_label.中。只要question_label不包含字符串&#34; con&#34;,继续前进。
  3. 迭代div .choice-row中的所有元素,并查找包含&#34; 1的值的那个。&#34;
  4. 对于只包含值1的.choice-row,找到相应的div&#34; ScoreSelectionText&#34;并将其存储在变量中。
  5. 然后我想将这些值输出到控制台(或.csv),其中打印了question_label,然后是ScoreSelectionText字符串值。

    我是Javascript / Jquery的新手,所以我很失落。这是我的尝试:

    $(".BlockOuter").each(function(){
      $(this).find(".QuestionNumber").each(function(){
          var question_label = $(this).text();
          $(this).find(".choice-row").each(function(){
            $(this).find("input").each(function(){
              if($(this).val() ==1){
                var checker = 1;
            }
    
            if (checker == 1){
              console.log(question_label);
              console.log($(this).find(".ScoreSelectionText").val());
          }
    
    });
    

    在这种情况下,我希望得到的输出为:

    {CB_3,&#34;劳工和农场工人倡导者&#34;}

1 个答案:

答案 0 :(得分:1)

您可以从.closest('.QuestionStructure')开始..我认为此代码会有所帮助

&#13;
&#13;
$(".BlockOuter").each(function(){
  $(this).find(".QuestionNumber").each(function(){   //<<< here
      var question_label = $(this).text();
      $(this).closest('.QuestionStructure').find(".choice-row").each(function(){
        $(this).find("input").each(function(){
          if($(this).val() == 1){
            console.log(question_label);
            console.log($(this).closest('.choice-row').find(".ScoreSelectionText").text());
          }
        });
      });
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="BlockOuter">
<div id="block_43557007" class="Block Standard">
<div class="BlockHeader">
<span>CB_3</span>
<div class="clear"></div>
</div>
<div class="QuestionsDiv">
<div class="Question" id="QID12">
<div class="SelectBorder">
<div class="QuestionStructure">
<div class="QuestionInfoBar">
   <div class="QuestionInfoBarInner">
      <div class="QuestionNumber" style="font-size: 13px;">CB_3</div>
   </div>
</div>
<div class="QuestionCanvas Skin OutlineMode">
   <div>
      <div class="QuestionText_Edit">
         <div class="QuestionText" id="QuestionText_q33690646_EN" title="Who is Cesar Chavez?" ng-non-bindable="true">Who is Cesar Chavez?</div>
      </div>
      <div class="InfoBarButtons"><a class="Button " clickcallback="BaseQuestion.clearAll:q33690646($evt,true)">Clear</a></div>
      <div class="InlineGrade">
         <div class="choice-row">
            <input class="form-control GradeInput GradePositive" size="1" value="1" questionid="q33690646" choiceid="1" validation="Number">
            <span>
               <div class="SelectionContainer SelectionPositive" id="ch230074" clickcallback="BaseQuestion.toggleGrade" instanceid="q33690646" p1="1" p2="" p3="" title="Click to toggle scoring">
                  <div class="ScoreSelection" clickcallback="BaseQuestion.toggleGrade" instanceid="q33690646" p1="1" p2="" p3="" title="Click to toggle scoring"><span class="ScoreSelectionText">Labor and farm worker advocate</span></div>
               </div>
            </span>
         </div>
         <div class="choice-row">
            <input class="form-control GradeInput" size="1" value="#" questionid="q33690646" choiceid="2" validation="Number">
            <span>
               <div class="SelectionContainer SelectionNeutral" id="ch3336258" clickcallback="BaseQuestion.toggleGrade" instanceid="q33690646" p1="2" p2="" p3="" title="Click to toggle scoring">
                  <div class="ScoreSelection" clickcallback="BaseQuestion.toggleGrade" instanceid="q33690646" p1="2" p2="" p3="" title="Click to toggle scoring"><span class="ScoreSelectionText">Salsa dancer</span></div>
               </div>
            </span>
         </div>
         <div class="choice-row">
            <input class="form-control GradeInput" size="1" value="#" questionid="q33690646" choiceid="3" validation="Number">
            <span>
               <div class="SelectionContainer SelectionNeutral" id="ch92975033" clickcallback="BaseQuestion.toggleGrade" instanceid="q33690646" p1="3" p2="" p3="" title="Click to toggle scoring">
                  <div class="ScoreSelection" clickcallback="BaseQuestion.toggleGrade" instanceid="q33690646" p1="3" p2="" p3="" title="Click to toggle scoring"><span class="ScoreSelectionText">Television personality</span></div>
               </div>
            </span>
         </div>
         <div class="choice-row">
            <input class="form-control GradeInput" size="1" value="#" questionid="q33690646" choiceid="4" validation="Number">
            <span>
               <div class="SelectionContainer SelectionNeutral" id="ch7917009" clickcallback="BaseQuestion.toggleGrade" instanceid="q33690646" p1="4" p2="" p3="" title="Click to toggle scoring">
                  <div class="ScoreSelection" clickcallback="BaseQuestion.toggleGrade" instanceid="q33690646" p1="4" p2="" p3="" title="Click to toggle scoring"><span class="ScoreSelectionText">Former Mexican president</span></div>
               </div>
            </span>
         </div>
      </div>
   </div>
</div>
<div class="clear"></div>
</div>
</div>
</div>
<div class="Question" id="QID13">
<div class="SelectBorder">
<div class="QuestionStructure">
<div class="QuestionInfoBar">
   <div class="QuestionInfoBarInner">
      <div class="QuestionNumber" style="font-size: 11px;">CB_3_con</div>
   </div>
</div>
<div class="QuestionCanvas Skin OutlineMode">
   <div>
      <div class="QuestionText_Edit">
         <div class="QuestionText" id="QuestionText_q76664205_EN" title="Please indicate how confident you are in the above answer" ng-non-bindable="true">Please indicate how confident you are in the above answer</div>
      </div>
      <div class="InfoBarButtons"><a title="Hold alt key to reverse code, plz" class="Button " clickcallback="BaseQuestion.scoreAll:q76664205($evt,null,true)">Auto</a></div>
      <div class="InlineGrade">
         <div class="choice-row">
            <input class="form-control GradeInput" size="1" value="#" questionid="q76664205" choiceid="1" validation="Number">
            <span>
               <div class="SelectionContainer SelectionNeutral" id="ch74193948" clickcallback="BaseQuestion.toggleGrade" instanceid="q76664205" p1="1" p2="" p3="" title="Click to toggle scoring">
                  <div class="ScoreSelection" clickcallback="BaseQuestion.toggleGrade" instanceid="q76664205" p1="1" p2="" p3="" title="Click to toggle scoring"><span class="ScoreSelectionText">My confidence</span></div>
               </div>
            </span>
         </div>
      </div>
   </div>
</div>
<div class="clear"></div>
</div>
</div>
</div>
</div>
</div>
<div class="BlockShadow"></div>
</div>
&#13;
&#13;
&#13;

注意:请注意控制台上的错误..您错过了更多});