如何使用jQuery在选择HTML控件之外放置一个删除按钮图标
How to place a delete button icon besides select HTML control using jQuery?
我的jQuery代码如下:
$(function () {
$(document).delegate('.products','click',function (e) {
var table_id = $(this).closest('table').attr('id');
var no = table_id.match(/'d+/)[0];
var first_row = $('#'+table_id).find('tbody tr:first').attr('id');
var new_select = $('#'+first_row).children('td:first').find('select:first').clone();
var tbody = $('#' + table_id + ' tbody');
var n = $(this).closest('table').find('select.prod_list').length+1;
new_select.attr('id','product_id_'+no+'_'+n);
new_select.attr('name', 'product_id_'+no+'['+n+']');
$('#'+table_id).find('tbody tr:first').children('td:first').append(new_select);
$('<button style="color:#C00; opacity: 2;margin-top: 6px;" type="button" class="close delete" data-dismiss="alert" aria-hidden="true">×</button>').appendTo( $(new_select.find('select')) );//Here I'm having an issue in placing the delete icon button
});
});
您可以看到,我正试图将删除图标按钮附加到新添加的<select>
,但我没有。有人能在这方面帮助我吗?js的小提琴是:http://jsfiddle.net/L247t/
您应该使用.insertAfter()
而不是.appendTo()
。
$('<button style="color:#C00; opacity: 2;margin-top: 6px;" type="button" class="close delete" data-dismiss="alert" aria-hidden="true">×</button>').inertAfter( $(new_select.find('select')) );
您正在尝试在select字段中添加按钮。这不可能。在select字段中,你只能添加options。
你可以做两件事:
// Adding a delete button after the select-field
new_select.after('<button style="color:#C00; opacity: 2;margin-top: 6px;" type="button" class="close delete" data-dismiss="alert" aria-hidden="true">×</button>');
或
// Appending the select field with an delete option
new_select.append('<option value="del">×</option>');
我在这里更新了你的小提琴:http://jsfiddle.net/L247t/1/
相关文章:
- 以最接近的图标为目标,将其更改为另一个图标
- 我是否可以在单击特定图标时更改地图标记图标,然后在单击另一个图标时切换,等等
- 是否可以用D3在圆环图的弧中插入一个图标
- 如何将一个图标转换为另一个图标
- 下一个上一个图标在猫头鹰转盘2中不起作用
- 如何在Episys Symitar中为HTML/Jquery选项卡添加一个图标
- 在ng-click上显示一个图标
- 是否有可能填充一个图标的百分比
- 我可以改变引导崩溃,只显示/展开时,单击一个图标
- 在双击一个图标,弹出和返回屏幕都正在褪色
- 是否可以在工具栏中为firebreath插件创建一个图标
- 显示上一个和下一个图标在弹出图像
- 我可以检索一个图标从地方.sqlite
- 为网站选择一个图标形式的图标包
- 如何更新一个图标,vimeo风格
- 需要弹跳一个图标/按钮没有jQuery UI
- 为什么当我点击一个图标时,会附加2个元素
- 如何在单击复选框时显示下一个图标
- 一个图标上的Javascript,用于展开和折叠不使用回车按钮的表
- jQuery Mobile:尝试在某些文本中间显示一个图标