开发者

How to select all inputs that autogenerate on a form and pick the last one?

I have a form into which the user can add more fields to fill by clicking on the "add" anchor (#addLink). However I also have a table with possible data and the purpose of it is so that if the user adds a new row of fields he can click then the row on the table that fits the most and the fields fill themselves. I can't seem to be able to do this though because I can't get a function that checks how many fields are and just fills the last one (I have to know how many there are to fill the last one and not replace everything else). The fields were created by another programmer and I don't have access to that code. He doesn't give me id's or classes just names which are of the form "links[n][url]" (where n = a number and the word url literally).

So far I had:

$('#addLink').click(funct开发者_如何学Goion(){
        //links[n][url]
        var $inputs = $('#links :input'); //#links is a div that contains the fields
        var names = {};

        $inputs.each(function(index){
            alert(index + ' : ' + $(this).attr('id'));
            ids[$(this).attr('name')];
        });
    });

but it just alerts (0 : ); nothing else. What am I missing?


$('#addLink').click(function(){

    var inputs = $('#links:input:last'); //#links is a div that contains the fields
    var names = {};


        alert(inputs.index() + ' : ' + $(inputs).attr('id'));


});

Are you just trying to select the last one? :inputs:last


Why you use #links if it the name of field, you might use $("input[name^='links']"); if the fields name start by links.

Regards,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜