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

I am trying to set the value of a js variable when clicking a link.This is what I have attempted so far

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");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745116589a4612163.html

相关推荐

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

    I am trying to set the value of a js variable when clicking a link.This is what I have attempted so far

    7小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信