简体   繁体   English

使用jQuery向表添加动态href

[英]Add dynamic href to table with jquery

I have a table with a template to insert rows, I would like to make those rows clickable so that I can edit them. 我有一个带有模板的表,用于插入行,我想使这些行可单击,以便我可以对其进行编辑。 How do I append an href value to the template? 如何将href值附加到模板?

My Template 我的范本

        <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 我的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;
   }

You can use .data() 您可以使用.data()

 row.find('.item_ref').data('ref','hello');

with

 <span class="item_ref" data-ref="" > Edit</span>

Then you can use it like -- 然后您可以像-

     console.log($('.item-ref').data('ref'));

If you just wish to store data somehow then this might be useful. 如果您只是希望以某种方式存储数据,那么这可能会很有用。 Let me know if there's something more you want to do. 让我知道您是否还有其他想做的事情。 Or what kind of data href holds and how you want to use it further. 或保留什么样的数据href ,以及您想如何进一步使用它。


UPDATE UPDATE

From what I understand up till now is, you want to add rows dynamically that needs to editable after insertion. 据我所知,您想要动态添加在插入后需要编辑的行。 Each row contain some fields with certain values. 每行包含一些具有某些值的字段。 And you want to save ref in item_ref class. 并且您想将ref保存在item_ref类中。

So here's how you can do it - 因此,这是您的操作方法-

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')); 
});

I've added a button to give demonstration. 我添加了一个button进行演示。 This approach definitely avoid unnecessary DOM elements like hidden inputs to be added for each row. 这种方法绝对避免为每行添加不必要的DOM元素(例如隐藏输入)。 With .data() you same multiple kind of information for every field like - 使用.data()您可以为每个字段提供多种相同的信息,例如-

 $("span").data('key_1', value_1);
 $("span").data('key_2', value_2);
 $("span").data('key_2', value_3);

fiddle for demonstration 摆弄示范

I think that's what you want to do and should serve the purpose. 我认为这就是您想要做的并且应该达到目的。 :) :)

There are actually a few ways to do this, one of them being: 实际上,有几种方法可以做到这一点,其中一种是:

  1. Add some inputs to your template that are hidden 向您的模板添加一些隐藏的输入
  2. Bind a click event to the row that will hide the spans and show the input 将click事件绑定到将隐藏跨度并显示输入的行

You would of course need a save button and do something with the values, but I didn't do that part. 您当然需要一个保存按钮并使用这些值来做一些事情,但是我没有做那部分。

A condensed not fully working demo: http://plnkr.co/edit/GGM0d9wfNcoZBd5kKCwA 浓缩的无法完全正常运行的演示: 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: 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;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM