创建动态文本框ID

时间:2016-03-29 11:53:13

标签: jquery

我想使用jquery在按钮的click事件上创建动态文本框id。我创建了一个函数,当我单击按钮时会创建三个文本框。我需要的是,每个文本框都应该有唯一的ID。

这是功能: -

function CreateTextBox(value) {
    return '<div class="col-md-12"><div class="col-md-3"><label>Ingredients</label><input class="form-control"name = "text1" id="ring" type="text" value = "' + value + '" />&nbsp;</div>'+
    '<div class="col-md-3"><label>Quantity</label><input class="form-control"name = "text2" type="text" id="rqty" value = "' + value + '" />&nbsp;</div>'+
    '<div class="col-md-3"><label>Measure Unit</label><input class="form-control"name = "text3" id="runit" type="text" value = "' + value + '" />&nbsp;</div>'+
    '<div class="col-md-3"><input type="button" value="Remove" class="btn btn-info remove" id="rm"/></div></div>' 

}

此代码首次适用于我。当我第二次运行此功能时,我需要不同的Id(如ring1,rqty1,runit1)

2 个答案:

答案 0 :(得分:1)

你可以在创建的父div中添加一个类(例如<div class="col-md-12 createdBoxes">),然后在函数中使用以下代码获取它们的数量:

var number=$('.createdBoxes').length+1;

然后将该数字添加到动态创建的字段id中,以便每个id都像id="qty'+number+'",并且在每次调用函数时,长度都会增加,因此您将获得动态创建的div数量的索引。

答案 1 :(得分:0)

你应该制作某种增量值来跟踪按钮按下的次数(例如 File filePath = getFileStreamPath("news_image"); shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(filePath)); shareIntent.setType("image/png"); shareIntent.putExtra(Intent.EXTRA_TEXT, "Here is my IMAGE"); startActivity(Intent.createChooser(shareIntent, "Share IMAGE Using...")) 之类的东西)。然后,每次添加输入时,您都可以使用{{1}}。