如何使按钮确定其表位置,调用函数并向它发送参数
How do I make a button determine its table position, call a function and send arguments to it?
我在 HTML/PHP 中创建了一个带有 ID-Name-Button 列的表。现在,当单击特定行的按钮时,我想调用一个函数,该函数为我构建一个字符串,然后打开字符串中包含的链接。
我遇到的两个问题是:
-
如何管理获取按钮的当前表格位置并获取前 2 个单元格中的数据?
-
如何将该数据提供给我的 JavaScript 函数,然后返回它?(我有如何构建URL的功能,我只需要表数据来完成它)
抱歉,如果这些看起来很愚蠢的问题,我对 PHP/Javascript 很陌生。
编辑:以下是我生成表格的方式(顺便说一下,它与Facebook相关,创建了一个朋友列表):
echo "<form id='xy' method='post' action='index.php'>";
echo "<table><tbody><tr><th>ID</th><th>Name</th><th>Button</th>";
for ($i = 0; $i < count($friends[data]); ++$i)
{
echo "<tr><td>".$friends[data][$i]["id"]."</td><td>".$friends[data][$i]["name"]."</td><td><input type='submit' onClick='BuildFeedURL()' value='Nachricht senden (FeedDialog)'/></td></tr>";
}
echo "</tbody></table>";
echo "</form>";
}
Boehmi,没有人是愚蠢的。这两个问题都可以使用JQUERY轻松解决
在此之前,您必须稍微更新您的 HTML:
echo "<tr><td class='fids'>".$friends[data][$i]["id"]."</td><td class="fname">".$friends[data][$i]["name"]."</td><td><input type='submit' onClick='BuildFeedURL(this)' value='Nachricht senden (FeedDialog)'/></td></tr>";
注意添加到TD的类,并this
到buildfeedURL(this);我不知道你为什么使用input type='submit'
,因为你可以使用javascript重定向/重新加载页面。
使用 jquery 的父级并找到:
buildfeedurl(obj) {
var fid = $(obj).parent().parent().find('.fid').html(); // .html() can be replaced with .text() too
var fname = $(obj).parent().parent().find('.fname').html(); // .html() can be replaced with .text() too
{
// do your processing of url building
}
// here you can redirect to your required URL
window.location.href = "http://stackoverflow.com";
}
无需将值传递给另一个函数。
使用jquery很容易
$('button').click(function(){
var td= $(this).closest('tr').find('td');
var id=$(td[0]).html();
var name=$(td[1]).html();
var yourstring = 'id="'+id+'" name="'+name+'"';
alert(yourstring);
});
演示
相关文章:
- 函数参数中的数据与指定变量之间的任何性能差异
- AngularJS:我可以跳过函数参数回调吗
- 为什么不'我们在javascript中使用函数参数的数据类型
- 使用闭包共享构造函数参数
- 函数参数的值错误
- 当一个重要的构造函数参数丢失时应该发生什么
- 阻止ReSharper将JavaScript函数参数放到新行中
- 根据函数参数将CSS值存储在var中,然后对其进行操作
- 使构造函数参数具有ES6类方法的特权
- 当客户端将两个按钮的javascript函数参数修改为相同时,SQL/PHP会更新多行
- Windows.Networking.EndpointPair构造函数参数
- 是否可以使用Sinon.JS检查函数参数绑定是否正确
- 大括号中的Javascript函数参数
- jQuery 无法使用 AJAX 调用访问函数内部的函数参数
- 将JSON字符串转换为函数参数
- eval()读取函数参数有多糟糕
- Javascript不能在一个命令中使用函数参数
- 函数参数在内部不可见,返回新的Promise(函数(resolve,reject).传递给被promise化的函数的
- ES6类构造函数参数
- 是否可以在javascript函数参数列表中调用多个函数