添加动态href表与jquery
Add dynamic href to table with jquery
我有一个模板插入行表,我想使这些行可点击,以便我可以编辑它们。我如何追加一个href值到模板?
我的模板
<tr class="template" style="display:none;">
<td><span class="item_num"> Num </span></td>
<td><span class="item_desc"> Description </span></td>
<td><span class="item_price"> Price </span></td>
<td><span class="item_ref">ref</span></td>
</tr>
My Javascript
var newRow = $('#quote .template').clone().removeClass('template');
var quoteItem = {
number: 1,
description: myDescriptionVariable,
price: myPriceVariable,
};
template(newRow, quoteItem)
.insertAfter('#quote tr.template')
.fadeIn()
function template(row, quoteItem) {
row.find('.item_num').text(quoteItem.number);
row.find('.item_desc').text(quoteItem.description);
row.find('.item_price').text(quoteItem.price);
row.find('.item_ref').attr('href','hello');
return row;
}
您可以使用.data()
row.find('.item_ref').data('ref','hello');
<span class="item_ref" data-ref="" > Edit</span>
然后你可以像这样使用——
console.log($('.item-ref').data('ref'));
如果你只是希望以某种方式存储数据,那么这可能是有用的。如果你还想做什么就告诉我。或者href
保存了什么类型的数据,以及您希望如何进一步使用它。
据我所知,到目前为止,你想动态添加行,需要在插入后编辑。每行包含一些具有特定值的字段。你想保存ref在item_ref
类。
你可以这样做-
var num = 1;
var myDescriptionVariable = 111;
var myPriceVariable = 999;
// You may have some other element triggers cloning
$("button").click(function(){
var newRow = $('#quote .template').clone().removeClass('template');
var quoteItem = {
number: num,
description: 'Description ' + myDescriptionVariable, // added to distinguish items
price: myPriceVariable + ' USD', // added to distinguish items
linkToPopup: myDescriptionVariable + '_link_goes_here' // added to distinguish items
};
template(newRow, quoteItem)
.insertAfter('#quote tr.template')
.show();
});
function template(row, quoteItem) {
row.find('.item_num').text(quoteItem.number);
row.find('.item_desc').text(quoteItem.description);
row.find('.item_price').text(quoteItem.price);
// here 'href' will hold desired link_to_popup
row.find('.item_ref').data('href',quoteItem.linkToPopup);
myDescriptionVariable+= 1; // added to distinguish items
myPriceVariable+=2; // added to distinguish items
num+=1; // added to distinguish items
return row;
}
$("#quote").on("click", ".item_ref",function(){
// this will give to desired link_to_pop_val
alert($(this).data('href'));
});
我添加了一个button
来演示。这种方法绝对避免了不必要的DOM元素,比如为每行添加隐藏输入。使用.data()
,您可以为每个字段提供相同的多种信息,例如-
$("span").data('key_1', value_1);
$("span").data('key_2', value_2);
$("span").data('key_2', value_3);
小提琴示范
我认为这就是你想要做的,并且应该达到目的。:)
实际上有几种方法可以做到这一点,其中之一是:
- 在模板中添加一些隐藏的输入
- 将点击事件绑定到行,将隐藏跨度并显示输入
你当然需要一个保存按钮并对这些值做些什么,但我没有做那部分。
一个浓缩的不完全工作的演示:http://plnkr.co/edit/GGM0d9wfNcoZBd5kKCwA
<tr class="template" style="display:none;">
<td><span class="item_num"> Num </span><input type="text" style="display:none" /></td>
<td><span class="item_desc"> Description </span> <input type="text" style="display:none" /></td>
<td><span class="item_price"> Price </span><input type="text" style='display:none' /></td>
<td><span class="item_ref">ref</span><input type="text" style='display:none' /></td>
</tr>
jquery: $(document).on('click', '#quote tr', function(e) {
$('span', this).hide();
$('input', this).show();
});
$('#add').on('click', function(e) {
var newRow = $('#quote .template').clone().removeClass('template');
var quoteItem = {
number: 1,
description: 'myDescriptionVariable',
price: 100,
};
template(newRow, quoteItem)
.insertAfter('#quote tr.template')
.fadeIn()
});
function template(row, quoteItem) {
row.find('.item_num').text(quoteItem.number).next().val(quoteItem.number);
row.find('.item_desc').text(quoteItem.description).next().val(quoteItem.description);
row.find('.item_price').text(quoteItem.price).next().val(quoteItem.price);
row.find('.item_ref').attr('href','hello').next().val('hello');
return row;
}
相关文章:
- 如何将 Javascript/Jquery 函数与 html 表单连接起来
- 如何正确地将jQuery表与金字塔模板集成
- 如何将表与 jquery 一起使用
- JQuery数据表与KnockoutJS
- 简单的大小写不敏感的搜索框的一个表与jQuery
- 文件上传与jquery表单插件和mvc 3是不填写与我的部分视图的目标属性
- 如何使用Jquery表单与php while循环不止一次
- jQuery匹配表与数组
- jQuery表单验证与邮件检查功能冲突
- 添加动态href表与jquery
- 问题,而使用jQuery数据表与特殊的表头
- 我如何动态地填充一个转置HTML表与JQuery
- 如何动态打印到另一个表与JQuery/Javascript
- 切割文本在表与PHP或jQuery/html/css
- 是否有一种方法来防止提交与此javascript/jquery表单
- 使用jquery手风琴与表列
- Ajax验证与jquery表单验证插件
- 如何制作一个Jquery表单?与面板上的持久值更改
- Jquery数学与多个HTML表
- 显示和隐藏表与过滤后的内容提交(jQuery)