Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi friends,
I have made a dynamic table in which new index is created by default but while i am going to fetch the value of particular index of object value then it always taking the first index value of that objects.

Here is the code
HTML
<head>
<script language="javascript" src="js/jquery.min.js"></script>
<SCRIPT>
    RowCount=1;
    stat=false;
    function AddRow()
    {
 
        jQtable=$('#SaleTable');
        jQtable.each(function()
        {
            var LastRow="<TR><TD align=center><INPUT TYPE='text' name='Slno_"+RowCount+"' id='Slno_"+RowCount+"' size=1></TD><TD align=center><INPUT type='checkbox' name='in_travel_"+RowCount+"' ID='in_travel_"+RowCount+"' value='in_terval'  size=10></TD><TD align=center><INPUT style='text-align:right' type='TEXT' name='date_"+RowCount+"' ID='date_"+RowCount+"' size=10></TD><TD align=center><textArea name='cities_visit_"+RowCount+"' ID='cities_visit_"+RowCount+"' cols=10></textArea></TD><td align=center><select id='reimbursement_"+RowCount+"' name='reimbursement_"+RowCount+"'><option selected='selected' value='None'>--None--</option><option value='DA'>DA</option><option value='Ticket'>Ticket</option><option value='Misc'>Misc</option></select></td><TD align=center><textArea name='Remark_"+RowCount+"' ID='Remark_"+RowCount+"' cols=10></textArea></TD><TD align=center><INPUT style='text-align:right' type='checkbox' name='bills_"+RowCount+"' ID='bills_"+RowCount+"' size=10></TD><TD align=center><INPUT style='text-align:right' type='submit' name='submit_"+RowCount+"' ID='submit_"+RowCount+"' onclick='getvalue()' value='Submit' size=10></TD><TD align=center></TR>";
            $(this).append(LastRow);
            });
RowCount++;
    }
    function removeTableRow()
    {
        jQtable=$('#SaleTable');
        if(RowCount > 1)
        {
            jQtable.each(function()
            {
                if($('tbody', this).length > 0)
                {
                    $('tbody tr:last', this).remove();
                }
                else
                {
                    $('tr:last', this).remove();
                }
            });
            RowCount--;
        }
 
    }
	function getvalue()
	{
	////var inp= $('input:radio[name=PatientPreviouslyReceivedDrug]:checked').val();
		alert(RowCount);
var sl= $('input:text[name=Slno_+ RowCount]').val();
alert(sl);
var in_tra= $('input:checkbox[name=in_travel_+ RowCount]:checked').val();
alert(in_tra);
//var slect=$('input:select[name=reimbursement_+ RowCount]:selected').val();

var slect=$("#reimbursement_+ RowCount option:selected").val();
alert(slect);
var val= $('input:text[name=Slno_2]').val();
alert(val);

}
</script>
</head>
<body>
</body>


here,variable RowCount getting a new value every time but when i am going to access it on function getvalue()
Alert(RowCount);
value is changing in every time
but the value of

var sl= $('input:text[name=Slno_+ RowCount]').val(); <----- here is always same

So the question is how to change the value of Slno_+<-- RowCount--> this varable

So that it take as if RowCount=2 then variable should be slno_2

Thanks
Posted
Updated 29-Aug-12 19:45pm
v2

Simply by putting RowCount outside of the string:
JavaScript
var sl= $('input:text[name=Slno_' + RowCount + ']').val();


Keep this in a loop such that as RowCount changes, value of name changes from Slno_1 to Slno_2, Slno_3, etc.
 
Share this answer
 
v2
Comments
[no name] 30-Aug-12 1:40am    
thanks Sandeep Mewara.
But the value in
alert(RowCount) Keep changing every time but "Slno_' + RowCount" not of this.

If i will put in a loop it will take from stating to end..

Can you explain by some code..
Thanks
Sandeep Mewara 30-Aug-12 1:58am    
Did you see/read what I said. You have not kept RowCount outside string accessing it. Alert shows changed values because it is without quotes around it.

Loop comment was to test it. Use as per you need. All you need is to form the text name variable properly. Already shared how you can do that.
alert("Slno_"+RowCount); // try this!
[no name] 30-Aug-12 2:39am    
Not working..
Thanks
C#
Use this
 $(LastRow).find(':submit').click(getvalue).end().appendTo('#SaleTable');

instead of

 $(this).append(LastRow);

and
function getvalue() {
    var row = $(this).closest('tr');
    var sl = $('input:text[name^="Slno_"]', row).val();
    alert(sl);
    var in_tra = $('input[name^="in_travel_"]:checked', row).val();
    alert(in_tra);
    var slect = $('select[name^="reimbursement_"]', row).val();
    alert(slect);
}

instead of.

function getvalue()
    {
    ////var inp= $('input:radio[name=PatientPreviouslyReceivedDrug]:checked').val();
        alert(RowCount);
var sl= $('input:text[name=Slno_+ RowCount]').val();
alert(sl);
var in_tra= $('input:checkbox[name=in_travel_+ RowCount]:checked').val();
alert(in_tra);
//var slect=$('input:select[name=reimbursement_+ RowCount]:selected').val();

var slect=$("#reimbursement_+ RowCount option:selected").val();
alert(slect);
var val= $('input:text[name=Slno_2]').val();
alert(val);

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900