dynamically retrieve textbox values using foreach jquery which itself is dynamically created

喜你入骨 提交于 2019-12-25 00:09:40

问题


How to dynamically retrieve values from input text box :

   $(":input").each(function(){alert($(this).text());});

Case :

Dynamically column names are taken from xml source & binded in this manner :

<script type="text/javascript" language="javascript">
    $(document).ready(function(){

            $.ajax({
                type: "GET",
                url: "GetColumnNames.xml",
                dataType: "xml",
                success: function(xml) {
                    var cWidth =  100/(xml.getElementsByTagName("Column")).length;
                    $(xml).find('Column').each(function(){
                        var cId = $(this).find('Id').text();
                        var cName = $(this).find('Name').text();
                        $('<td id='+cId+' width='+cWidth+'>'+cName+'</td>').appendTo('#page-wrap');
                        $('<label for='+cName+'>'+cName+'<input type="text" name='+cName+'id='+cName+' value="" class="text ui-widget-content ui-corner-all" /></label>').appendTo('#moreItems');
                    });
                }
            });
        });
    </script>

The #moreItems is part of another div which is part of modal popup and i want to access values within that,

So,

after validation i go this way :

   if ( bValid ) {


                       $(":text").each(function(){
                        alert($(this).text());
                       });

}

But still i am not able to access input values.

Note :

It is not similar to :

How do I retrieve a textbox value using JQuery? , Jquery retrieve values of Dynamically created elements , how do i retrieve a textbox's title and name using jquery? ,

Looking for your help ..

Thanks.


回答1:


You're missing a $ sign. It should be:

$(":text").each(function(){
    alert($(this).val());
});

You should also try to generate well-formed HTML, with attributes delimited by quote characters:

$('<td id="'+cId+'" width="'+cWidth+'">'+cName+'</td>').appendTo('#page-wrap');
$('<label for="'+cName+'">'+cName+'<input type="text" name="'+cName+'" id="'
    +cName+'" value="" class="text ui-widget-content ui-corner-all" /></label>')
    .appendTo('#moreItems');


来源:https://stackoverflow.com/questions/4862976/dynamically-retrieve-textbox-values-using-foreach-jquery-which-itself-is-dynamic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!