获得<a> tag in javascript

时间:2016-01-15 06:48:27

标签: javascript jquery codeigniter

I have a javascript containing a form with image, radio button and text boxes. When I click on the delete hyperlink, I want to get the rel of that link.

How can I go about getting it?

Form

<script type="text/template" id="imageTemplate">
    <div class="row gc_photo" id="gc_photo_{{id}}" style=" border-bottom:1px solid #ddd; padding-bottom:20px; margin-bottom:20px;">
        <div class="col-md-2">
            <input type="hidden" name="images[{{id}}][filename]" value="{{filename}}"/>
            <img class="gc_thumbnail" src="<?php echo base_url('../product_images/{{filename}}');?>" style="padding:5px; border:1px solid #ddd"/>
        </div>
        <div class="col-md-10">
            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        <input name="images[{{id}}][alt]" value="{{alt}}" class="form-control" placeholder="<?php echo lang('alt_tag');?>"/>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="checkbox">
                        <label>
                            <input type="radio" name="primary_image" value="{{id}}" {{#primary}}checked="checked"{{/primary}}/> <?php echo lang('main_image');?>
                        </label>
                    </div>
                </div>
                <div class="col-md-4">
                    <a onclick="return remove_image($(this));" rel="{{id}}" class="btn btn-danger pull-right"><i class="icon-times "></i></a>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <label><?php echo lang('caption');?></label>
                    <textarea name="images[{{id}}][caption]" class="form-control" rows="3">{{caption}}</textarea>
                </div>
            </div>
        </div>
    </div>
</script>

Function to delete

function remove_image(img)
{
    if(confirm('<?php echo lang('confirm_remove_image');?>'))
    {
         var id  = img.attr('rel');
        alert(id);
    }
}

1 个答案:

答案 0 :(得分:1)

您可以在此处使用jQuery点击处理程序。从锚标记中删除onclick属性并在下面单击处理程序

$(function(){
   $('a.btn.btn-danger').click(function(){
     if(confirm('<?php echo lang('confirm_remove_image');?>'))
     { 
       alert($(this).attr('rel'));
     }
   });
});