1

I have a form with a text input field and option where user can add more than one text input fields (http://prntscr.com/1dikv3) This screenshot will show how it looks.

Now when ever a new field is added using js the name of the field is dynamic say default field has value field and new added field will be have field_2,field_3 and so on...

Now I want to get all the values of these field and insert into DB. I am not sure how can I get the values of all those different named input fields using array or loop?

Here is my JS code:

$(function() {
    var scntDiv = $('#p_scents');
    var i = $('#p_scents p').size() + 1;

    $('#addScnt').live('click', function() {
            $('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
            i++;
            return false;
    });

    $('#remScnt').live('click', function() { 
            if( i > 2 ) {
                    $(this).parents('p').remove();
                    i--;
            }
            return false;
    }); });

If someone can please guide me I will be thankful.

Thanks

4

5 に答える 5

0

確かに、コンテンツが複製されることがわかっている場合は、ID を使用しないでください。つまり、それらをクラスに変更します。

マークアップ

$('.p_scents') //クラスを使用

コード

$(function() {
    var scntDiv = $('.p_scents');
    var i = $('#p_scents p').size() + 1;

    $('#addScnt').live('click', function() {
            $('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
            i++;
            return false;
    });
 });
于 2013-07-04T14:42:35.790 に答える