如何在Jquery中调用父节点

问题描述 投票:0回答:2

我想访问每个Html动态控件如下,每个控件有Id Ref1_和Ref2_:

$('[id^="Ref1_"]').each(function () {
    if ($(this).val().length > 0) {
        var Temp = $(this).val() + ":" +$(this).parent().find("Ref2_").attr('id').split('_')[1]; 
        Data += Sp + $(this).attr("id").split('_')[1] + ":" + $(this).val();
        Sp = ',';
    }
});

我想结合每个价值动态控制,但发现不工作

C#代码:

        HtmlTableRow r = new HtmlTableRow();
        HtmlTableCell CRef1 = new HtmlTableCell(); 
        HtmlTableCell CRef2 = new HtmlTableCell();
        HtmlInputText Ref1 = new HtmlInputText();
        Ref1.ID = "Ref1_" + iSrl;
        Ref1.Attributes.Add("dir", "ltr");
        HtmlInputText ref2 = new HtmlInputText();
        ref2.ID = "Ref2_" + iMSrl;
        ref2.Attributes.Add("dir", "ltr");
        CRef1.Controls.Add(Ref1);
        CRef2.Controls.Add(ref2);
        r.Controls.Add(CRef1);
        r.Controls.Add(CRef2);
        this.Table.Controls.Add(r);
javascript jquery
2个回答
0
投票

var Data='', Sp='';
$('[id^="Ref1"]').each(function () {
    if ($(this).val().length > 0) {
        
        //console.log($(this).attr("id")+ " : " + $(this).val() + " - Ref2" + $(this).parent().parent().find('[id^="Ref2"]').val());
        
        var Temp = $(this).val() + ":" + $(this).parent().parent().find('[id^="Ref2"]').attr('id').split('_')[1]; 
        Data += Sp + $(this).attr("id").split('_')[1]+ " : " + $(this).val() + "^" + $(this).parent().parent().find('[id^="Ref2"]').val();
        Sp = ',';
    }
});

    console.log(Data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table id="Table">
  <tr>
    <td>
        <input name="Ref1_1330" type="text" id="Ref1_1330" dir="ltr" 
     value="2342" />
    </td>
    <td>
        <input name="Ref2_1330" type="text" id="Ref2_1330" dir="ltr" 
     value="23342" />
     </td>
  </tr>
  <tr> 
      <td><input name="Ref1_1474" type="text" id="Ref1_1474"   dir="ltr" value='657'/>
     </td>
      <td><input name="Ref2_1474" type="text" id="Ref2_1474" dir="ltr" value='957'  />
       </td>
        </tr>
</table>

你可以尝试这个代码


0
投票
`
 <table id="Table">
    <td>
    <input name="Ref1_1330" type="text" id="Ref1_1330" dir="ltr" 
     value="2342" /></td>
    <td>
    <input name="Ref2_1330" type="text" id="Ref2_1330" dir="ltr" 
     value="23342" />
     </td>
    </tr>
     <tr> 
      <td><input name="Ref1_1474" type="text" id="Ref1_1474"   dir="ltr" />
     </td>
      <td><input name="Ref2_1474" type="text" id="Ref2_1474" dir="ltr"   />
       </td>
        </tr>
        </table>
 `
© www.soinside.com 2019 - 2024. All rights reserved.