如何让 HTML 按钮值作为参数传递给我的 javascript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7205148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get HTML button value to pass as a parameter to my javascript?
提问by stackoverflow
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>
回答by canon
Setting aside the fact that you're placing a unique id in the value
attribute rather than the id
attribute... here's a fiddle.
撇开您在value
属性中而不是id
属性中放置唯一 id 的事实......这是一个fiddle。
$(document).ready(function(){
$("button").click(function(){
var me = $(this);
// do whatever with me
alert(me.val());
me.hide();
});
});
回答by James Allardice
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.
您在问题中发布的代码似乎存在很多问题。首先,(除非您将其<!DOCTYPE html>
用作文档类型),您不能id
使用以数字开头的值。
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.
其次,您的getUniqueButtonValue
函数中的 jQuery(我假设它是 jQuery 而不是其他一些 JS 库)将无法工作,因为选择器将寻找一个value
不太可能存在的元素。
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.
我假设value
your的属性button
对应于id
另一个元素的 ,当单击按钮时要隐藏它。
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:
由于您的示例中似乎有 jQuery 代码,因此我将为您提供一个 jQuery 解决方案,因为它要简单得多:
$(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.
此外,您没有关闭body
标签,但我猜这只是将代码复制并粘贴到问题中的错误。
回答by MaXo
You can do this:
你可以这样做:
<button id=123 value=uniqueId545 onclick="javascript:getUniqueButtonValue($(this).val());">Clear Selection</button>
回答by Pratik
try this
尝试这个
$("#123").click(function(){
getUniqueButtonValue($(this).val());
});
回答by mahulst
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:
我不确定你想在这里做什么。如果我猜对了,这就是你想要的(id 不应该以数字开头,所以我在 123 之前放了一个“a”:
$("#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>
回答by NobRuked
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:
使用以下 JavaScript:
$('.clearbutton').click(function(e) {
$($(this).val()).val('');
});