最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How do I get HTML button value to pass as a parameter to my javascript? - Stack Overflow

programmeradmin4浏览0评论

I have multiple buttons corresponding to multiple text areas to clear. I need to send to have a function to handle all of these buttons and handle each seperately

    <html>
    <head>
    <script src="jquery-1.6.js"></script>
    <script type="text/javascript">

        function getUniqueButtonValue(value)
        {
            alert(value);
            $("value").hide();
        }

    </script>
    </head>
    <body>

         <button id=someUinqueId value=something>Clear Selection</button>
    </body>
    </html>

I have multiple buttons corresponding to multiple text areas to clear. I need to send to have a function to handle all of these buttons and handle each seperately

    <html>
    <head>
    <script src="jquery-1.6.js"></script>
    <script type="text/javascript">

        function getUniqueButtonValue(value)
        {
            alert(value);
            $("value").hide();
        }

    </script>
    </head>
    <body>

         <button id=someUinqueId value=something>Clear Selection</button>
    </body>
    </html>
Share Improve this question edited Aug 26, 2011 at 13:32 stackoverflow asked Aug 26, 2011 at 13:11 stackoverflowstackoverflow 19.4k52 gold badges136 silver badges200 bronze badges 0
Add a comment  | 

6 Answers 6

Reset to default 5

Setting aside the fact that you're placing a unique id in the value attribute rather than the id attribute... here's a fiddle.

$(document).ready(function(){
    $("button").click(function(){
        var me = $(this);
        // do whatever with me
        alert(me.val());
        me.hide();
    }); 
});

There seem to be numerous problems with the code you've posted in your question. Firstly, (unless you're using <!DOCTYPE html> as your doctype) you can't have id values starting with a number.

Secondly, the jQuery (I'm assuming it's jQuery and not some other JS library) in your getUniqueButtonValue function is not going to work, because the selector is going to look for a value element, which is unlikely to exist.

I'm assuming that the value attribute of your button is meant to correspond to the id of another element, which you want to hide when the button is clicked.

As you have what appears to be jQuery code in your example, I will give you a jQuery solution to this, as it's far simpler:

$(document).ready(function() {
    $("button").click(function() {
        alert(this.value);
        $("#" + this.value).hide();
    });
});

Also, you don't close your body tag, but I'm guessing that's just a mistake in copying and pasting the code into the question.

You can do this:

<button id=123 value=uniqueId545 onclick="javascript:getUniqueButtonValue($(this).val());">Clear Selection</button>

try this

$("#123").click(function(){
    getUniqueButtonValue($(this).val());
});

I'm not sure what you are trying to do here. If i guess correctly this is what you want (ids shouldn't begin with a number so I put an 'a' before the 123:

$("#a123").click(function(){
    getUniqueButtonValue($("#a123").getAttribute('value');
}
function getUniqueButtonValue(value)
{
        alert(value);
        $("#"+value).hide();
}

</script>
</head>
<body>

     <button id=123 value=uniqueId545>Clear Selection</button>
</html>

You can save yourself a lot of work by trying:

<button class="clearbutton" value="#Foo">Clear Foo</button>
<input type="text" name="foo" id="foo" />

With the following JavaScript:

$('.clearbutton').click(function(e) {
    $($(this).val()).val('');
});
发布评论

评论列表(0)

  1. 暂无评论