如何动态地为 <input> 和选择器值指定 id 名称
How to give id names to <input> and selector values dynamically
我目前正在循环浏览列表并附加该信息,如下所示。我想使用该列表中的"ID"信息,并在每次按下"付费"按钮时将该信息发送到 URL。当我将选择器硬编码为"#input1"并将 id 值硬编码为 identity[0] 时,这目前有效。
如果我有第二组带有新 id 的信息和一个新按钮,这将不起作用,因为它在追加期间,第二个按钮也将是 id input1。
有没有办法在每次循环时为按钮动态提供不同的 ID 名称,将其与列表 ID 号配对,以及根据按下的按钮向选择器提供信息的方法?
或者有更好的方法可以做到这一点吗?请指教。谢谢。
var identity = [];
var count = 0;
$.getJSON("http://website.com/public/user/listunpaidbills/",function(data){
//Loop for each element on the data
$.each(data,function(elem){
var wrap = $("<div/>").attr('data-role', 'collapsible');
count++;
identity.push(data[elem].id);
//Create the h1 and the other elements appending them to bills List
$("<h1/>",{
text:data[elem].reference
}).appendTo(wrap);
$("<p/>",{
text:"Account: "+ data[elem].account
}).appendTo(wrap);
$("<p/>",{
text:"Amount: "+ data[elem].amount
}).appendTo(wrap);
$("<input type='submit' value='Paid' id='input1'/>",{
text:"Paid"
}).appendTo(wrap);
wrap.appendTo('#unpaidList');
})//end of for each loop
$( "#unpaidList" ).collapsibleset( "refresh" );
})
$("#input1").click(function(){
$.getJSON("http://website.com/public/user/confirmbill/", {
id: identity[0]
}, function(data) {
alert(data.status);
}).fail(function() {
alert("error");
})
});
如果我理解正确,最简单的解决方案是
将类添加到输入以将其选中
向同一输入添加数据属性,以标识所需的 identity[] 索引。
$("<input type='submit' value='Paid' id='input1'/>",{
text:"Paid"
成为
$("<input type='submit' value='Paid' />",{
class:"myinput",
text:"Paid"
}).appendTo(wrap)
.data('identityindex',count)
和
$("#input1").click(function(){
$.getJSON("http://website.com/public/user/confirmbill/", {
id: identity[0]
成为
$(".myinput").click(function(){
var $this = $(this);
var index = parseInt($this.data('identityindex'));
$.getJSON("http://website.com/public/user/confirmbill/", {
id: identity[index]
希望有帮助。
当您附加带有 Json 响应的按钮时。如果要使ID动态化,则必须将单击功能绑定到该按钮,请尝试:
$.getJSON("http://website.com/public/user/listunpaidbills/",function(data){
//Loop for each element on the data
$.each(data,function(elem){
//your code
var dynamicId = "input2";//local variable of dynamic button
$("<input type='submit' value='Paid' id='"+dynamicId+"'/>",{
text:"Paid"
}).appendTo(wrap);
wrap.appendTo('#unpaidList');
//binding click event to created button
$("#input1").bind('click',function(){
$.getJSON("http://website.com/public/user/confirmbill/", {
id: identity[0]
}, function(data) {
alert(data.status);
}).fail(function() {
alert("error");
})
});
})//end of for each loop
$( "#unpaidList" ).collapsibleset( "refresh" );
})
相关文章:
- 如何使HTML5颜色选择器返回颜色名称而不是颜色代码
- 如何为HTML属性/选择器创建全局名称空间或变量
- 如何在 Angular UI 日期选择器中格式化行和月份名称
- Jquery - 创建动态名称选择器
- 如何在 dojo.on 事件名称的选择器中使用 :not
- 从 jquery 地址选择器返回街道名称
- 如何在多日期选择器插件中显示月份名称
- 如何制作动态名称选择器(无限)
- 警告:失败的 propType:在“维度选取器”中未指定所需的属性“维度名称”.检查“连接(维度选择器)”的渲染方法
- 如何避免在jQuery中重复选择器名称
- 是否可以创建一个使用选择器名称的jQuery函数
- 使用属性等于选择器时转义元素名称中的方括号
- j查询名称选择器在 IE6 中不起作用
- JS/jQuery-动态随机名称选择器
- 函数中未识别名称选择器
- jQuery选择器按表单索引和输入名称
- CSS选择器按属性匹配元素's的名称开始
- 构造一个Asp.Net自动完成名称选择器
- Javascript名称数组输入选择器(name[sometext])
- 当状态树的两个分支具有相同的选择器名称时,如何管理redux选择器