Can this function be done with names rather than IDs?

This question already has an answer here:

  • How can I select an element by name with jQuery? 15 answers

  • Of course - using the attribute selector - and perhaps take the first if there are more.
    NOTE: Name is not a valid attribute of a DIV - now I see you are cloning divs in your fiddle

     var cloneCount = 1;
     $("button").click(function(){
       $('input[name="name0"]').eq(0) // only that one
          .clone()
          .attr('name', 'name'+ cloneCount++)
          .insertAfter($('[name^=name]:last'))
          .text('name ' + (cloneCount-1)); //<--For DEMO
     });
    

    If you need them to have the same name you could do

     $("button").click(function(){
       var $q = $('input[name="question"]').last();
       $q.clone().insertAfter(q);
     });
    
    链接地址: http://www.djcxy.com/p/36272.html

    上一篇: 无法使用Jquery设置TextArea值

    下一篇: 这个功能可以使用名称而不是ID来完成吗?