我正在将“鲍勃叔叔的有机食品”存储在data-Iname属性中,同时检索与“鲍勃叔叔”相同的值
以下是我用来从data-Iname检索值的过程
var iname = $(this).attr(“ data-Iname”);
这是我到目前为止所做的!
$(函数(){
var ItemId = 0;
$("#AddItems").change(function () {
var SelectedUserId = $("#AddItems").val();
var SelectedItem = $("#AddItems option:selected").text();
//alert(SelectedItem);
var ItemName = "Uncle Bob's Organic";
items.push(SelectedItem + '|' + SelectedUserId);
ItemId++;
//$("#tblItems").append("<tr id=" + ItemId + "><td id=" + ItemId + ">" + SelectedItem + ' ' + "<span class= 'glyphicon glyphicon-trash' title='Delete'></span><br></td></tr>");
$("#AddedItems").append("<div id=div_" + ItemId + ">" + SelectedItem + ' ' + "<span class= 'remove glyphicon glyphicon-trash' id=remove_" + ItemId + " title='Delete' data-Iname='" + ItemName.toString() + "'></span><br></div>");
});
});
$('.container').on('click', '.remove', function () {
var iname = $(this).attr("data-Iname");
var id = this.id;
var split_id = id.split("_");
var deleteindex = split_id[1];
for (var i = 0; i < items.length; i++)
{
if (iname == items[i])
{
alert(items[i])
items.splice(i, 1);
}
}
alert(items);
$("#div_" + deleteindex).remove();
});
最佳答案
试试这个代替
var ItemName = "Uncle Bob's Organic";
$("#AddedItems").append(
"<div id=div_" +
ItemId +
">" +
SelectedItem +
" " +
"<span class= 'remove glyphicon glyphicon-trash' id=remove_" +
ItemId +
" title='Delete' data-Iname=\"" +
ItemName.toString() +
"\"></span><br></div>"
);
所以您的输出类似于data-Iname ='Uncle Bob's Organic'..我只是用双引号替换了外部引号