admin管理员组文章数量:1244249
I am trying to figure out how to get the value of a button using JavaScript. There are several buttons generated using a while statement in php code, so the name
and id
are the same for all of them. The only difference between them is the value.
Part of my php code:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align=\"center\"><input type=\"button\" id=\"details\" name=\"details\" value=\"" . $row['mcn'] . "\" onClick=\"MCNdetails()\"></td>";
echo "</tr>";
}
What I'm trying to achieve is that when this button is clicked a JavaScript alert is displayed with this records details.
(FYI - "Faker" is the form name that this table is a part of.)
My JavaScript code:
function MCNdetails()
{
var buttonDetails = document.Faker.details;
var buttonValue = buttonDetails.value;
if (buttonValue === "2700-2057" )
{
alert ("Details are here");
return;
}
else
{
alert ("No Dice " + buttonValue);
return;
}
}
I have approx. 300 records so far. When I click on ANY of the buttons I always get the else statement. I did try getting the element by id like this:
function MCNdetails()
{
var buttonDetails = document.getElementById("details");
var buttonValue = buttonDetails.value;
if (buttonValue === "2700-2057" )
{
alert ("Details are here");
return;
}
else
{
alert ("No Dice " + buttonValue);
return;
}
}
But that would always return the value of the first button regardless of which one I clicked.
I have done some Google searching as well as searching this site and I can't find this same scenario where the buttons are generated.
Any help or pointers would be appreciated. Thank you for your time :)
I am trying to figure out how to get the value of a button using JavaScript. There are several buttons generated using a while statement in php code, so the name
and id
are the same for all of them. The only difference between them is the value.
Part of my php code:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align=\"center\"><input type=\"button\" id=\"details\" name=\"details\" value=\"" . $row['mcn'] . "\" onClick=\"MCNdetails()\"></td>";
echo "</tr>";
}
What I'm trying to achieve is that when this button is clicked a JavaScript alert is displayed with this records details.
(FYI - "Faker" is the form name that this table is a part of.)
My JavaScript code:
function MCNdetails()
{
var buttonDetails = document.Faker.details;
var buttonValue = buttonDetails.value;
if (buttonValue === "2700-2057" )
{
alert ("Details are here");
return;
}
else
{
alert ("No Dice " + buttonValue);
return;
}
}
I have approx. 300 records so far. When I click on ANY of the buttons I always get the else statement. I did try getting the element by id like this:
function MCNdetails()
{
var buttonDetails = document.getElementById("details");
var buttonValue = buttonDetails.value;
if (buttonValue === "2700-2057" )
{
alert ("Details are here");
return;
}
else
{
alert ("No Dice " + buttonValue);
return;
}
}
But that would always return the value of the first button regardless of which one I clicked.
I have done some Google searching as well as searching this site and I can't find this same scenario where the buttons are generated.
Any help or pointers would be appreciated. Thank you for your time :)
Share Improve this question edited Oct 30, 2012 at 3:38 NullPoiиteя 57.3k23 gold badges128 silver badges146 bronze badges asked Oct 30, 2012 at 3:32 Andrew FoxAndrew Fox 8904 gold badges13 silver badges34 bronze badges 2- @NullPointer Is it bad form on Stackoverflow to have a signature at the bottom of the post? I also noticed that if I started the post with "hello" it also got deleted. Thanks for the edit :) – Andrew Fox Commented Oct 30, 2012 at 5:29
- yes please dont use ... signature and hello , greeting and like that – NullPoiиteя Commented Oct 30, 2012 at 6:41
2 Answers
Reset to default 6try something like
<script type="text/javascript">
function getVal(value)
{
alert(value);
}
</script>
<input type="button" value="Add" onclick="getVal(this.value)">
Note that using the same id for more than one element is invalid html, though most (all?) browsers tend not to make a fuss about it and they'll still display the page quite happily. That's why when you tried to use document.getElementById("details")
it always gave you the first button with that id - how would the browser know which one you meant?
Having said that, if you pass this
in the function call on click:
onClick=\"MCNdetails(this)\"
...that will give the function a direct reference to the particular button that was clicked. So then:
function MCNdetails(btn) {
var buttonValue = btn.value;
// etc
}
In which case your buttons don't really need ids at all. But if you do keep the ids you should find a way to make them unique (e.g., append a sequence number to the end of each).
本文标签: phpHow to get button value using JavascriptStack Overflow
版权声明:本文标题:php - How to get button value using Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740203434a2240624.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论