本文介绍了从动态生成的HTML JQuery获取元素的Id /名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取 input 元素的 id name code> type =text来自动态生成的HTML。



我需要获取 < input name =ctl00 $ DefaultContent $ ctl2type =text>

  < TR> 
< td class =TextBold> 1.< / td>
< td class =TextBold>问题1的内容:< / td>
< td class =TextBold>
< input name =ctl00 $ DefaultContent $ ctl2type =textclass =TextNormal/>
< / td>
< / tr>

预先感谢

BB p>

解决方案

Descriptiom



<输入名称=ctl00 $ DefaultContent $ ctl2type =textclass =TextNormal/> 没有 id 属性。但是,您可以使用此选择器获取每个输入类型文本的集合 $(input [type ='text'])。我想你想得到 name 属性。



查看示例和。

样本



  $(function(){
$(input [type ='text'])。each(function(){
alert(id =+ $(this).attr(id));
alert(name =+ $(this).attr(name));
}) ;
});



更多信息



'UL>





  • 更新



    您的输入元素有一个属性。您可以使用

      $( class>  (id =+ $(this).attr(id)); 
    alert(name =+ $(this).TextNormal)。 .attr(name));
    });


    How can I get the id or name of input element of type="text" from dynamically generated HTML.

    I need to get the Id of <input name="ctl00$DefaultContent$ctl2" type="text">

     <tr>
        <td class="TextBold" >1.</td>
        <td class="TextBold" >Content for Question 1:</td>
        <td class="TextBold" >
          <input name="ctl00$DefaultContent$ctl2" type="text" class="TextNormal" />
        </td>
      </tr>
    

    Thank in advance

    BB

    解决方案

    Descriptiom

    Your <input name="ctl00$DefaultContent$ctl2" type="text" class="TextNormal" /> has no id attribute. But you can get a collection of every input type text using this selector $("input[type='text']). I think you want to get the name attribute.

    Check out the sample and this jsFiddle Demonstration.

    Sample

    $(function() {
        $("input[type='text']").each(function() {
           alert("id = " + $(this).attr("id"));
           alert("name = " + $(this).attr("name"));
        });
    });
    

    More Information

    Update

    Your input element has a class attribute. You can get all elements that has this class using

    $(".TextNormal").each(function() {
        alert("id = " + $(this).attr("id"));
        alert("name = " + $(this).attr("name"));
    });
    

    这篇关于从动态生成的HTML JQuery获取元素的Id /名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-05 12:29
    查看更多