0

I have dynamically generated textboxes. I want to assign names and id to the textboxes and also retrieve the values of dynamically generated textboxes to insert into database. How can it be done?

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>

<select id="sel2" multiple>
<option value="first">first option</option>
<option value="second">sec option</option>
<option value="third">third option</option>
<option value="fourth">fourth option</option>
<option value="fifth">fifth option</option>
<option value="sixth">six option</option>
</select>


<table id="tab2">
<tbody>

</tbody>
</table>

<button id="add" value="Add">Add</button>

<script>
$(document).ready(function()
{

$("#add").click(function()
{
    var selectval = $('#sel2').val();

    var len = selectval.length;

    for(var i=0; i < len; i++){
        $("#tab2 tbody").append("<tr><td>"+selectval[i]+"</td><td><input type='text'</td></tr>");
    }

});
});
</script>
4

1 に答える 1

0

Try like below , It will help you...

$("#tab2 tbody").append("<tr><td>"+selectval[i]+"</td><td><input type='text' id='text" + i + "' /></td></tr>");

It will generate Id like text0, text1, text2...

To Get the value from the Text box add the below codes and try it...

Fiddle Example : http://jsfiddle.net/RDz5M/4/

HTML :

<button id="Get" value="Get">Get</button>

JQuery :

$("#Get").click(function(){
$('#tab2 tr').each(function(){
    $("#divResult").html($("#divResult").html() + ($(this).find("input[id^='text']").val()) + "<br>")
});
});
于 2013-03-15T17:42:38.427 に答える