jQuery动态添加input,name值自增
Posted zhangjianzhen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jQuery动态添加input,name值自增相关的知识,希望对你有一定的参考价值。
function addLi(){ var listNum=$(".divList").length; var num= listNum == 0 ? 1: listNum+1 ; console.log(num); var divObj = ‘<div class="divList"><input class="small_input" placeholder="起始年份" name="start_year‘+num +‘" /><input class="small_input" placeholder="结束年份" name="end_year‘+num +‘" /><input class="small_input" placeholder="国家" name="country‘+num +‘" /><input class="small_input" placeholder="项目金额" name="item_amount‘+num +‘" /><input class="small_input" placeholder="项目名称" name="item_name‘+num +‘" /></div>‘ $(‘.main_business‘).append(divObj); var input_sum_val=$(".input_sum").val() $(".input_sum").val(++input_sum_val); }
<dd style="height: auto;"> <label>主营业务</label> <input class="input_sum" type="hidden" name="max" value="1" /> <div class="main_business"> <input class="small_input" placeholder="起始年份"><input class="small_input" placeholder="结束年份"> <input class="small_input" placeholder="国家"><input class="small_input" placeholder="项目金额"><input class="small_input" placeholder="项目名称"> </div> <button onclick="addLi()">add</button> </dd>
以上是关于jQuery动态添加input,name值自增的主要内容,如果未能解决你的问题,请参考以下文章