javascript - Set variable equal to this value on click - Stack Overflow

admin2025-04-20  0

I am trying to set the value of a js variable when clicking a link.

This is what I have attempted so far (simplified):

<a href="#?id=1" value="1" class="delete_link">Click to delete this row</a>

$(document).ready(function(){
    $(".delete_link").click(function(){
        var deleteID = $(this).val();
        alert(deleteID);

    });
}):

But this does not set the variable deleteID to 1. Am I selected the data in the link incorrectly?

Heres a jsfddle: link

I am trying to set the value of a js variable when clicking a link.

This is what I have attempted so far (simplified):

<a href="#?id=1" value="1" class="delete_link">Click to delete this row</a>

$(document).ready(function(){
    $(".delete_link").click(function(){
        var deleteID = $(this).val();
        alert(deleteID);

    });
}):

But this does not set the variable deleteID to 1. Am I selected the data in the link incorrectly?

Heres a jsfddle: link

Share Improve this question asked Apr 26, 2013 at 15:58 crmephamcrmepham 4,76019 gold badges87 silver badges163 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 7

Try:

var deleteID = $(this).attr("value");

DEMO: http://jsfiddle/CjS8k/3/

value isn't a valid attribute for <a> elements, meaning it won't be put into the .value property (which is what .val() will return). So instead, use:

<a href="#?id=1" data-value="1" class="delete_link">Click to delete this row</a>

with:

var deleteID = $(this).attr("data-value");

DEMO: http://jsfiddle/CjS8k/4/

there "value" is an attribute.

change your line to

 var deleteID = $(this).attr("value");
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745116590a285897.html

最新回复(0)