php - 使用 javascript 时将函数值传递给输入 id

标签 php javascript

enter image description here

大家好。

根据上图,我有一个从 MySql 数据库获取数据的表,我想在表结果中编辑该表上的字段。我正在学习使用 javascript,所以请友善。在我的 table 上,您可以看到 ID # 16 有一个 currentbal 输入字段和一个图像按钮,单击即可调用我的函数并将 ID 的值传递给它。该 ID 与我的数据库上的记录 ID 相对应,因此我知道要编辑哪条记录。

无论如何,这是我的 js 函数:

function funcEdit(row_id){  
    var currentbal = document.getElementById('currentbal').value;
    alert(currentbal);   
}

它非常适合我的第一行,但如果我调用第二行,我就会遇到问题,它仍然显示的 currentbal 值是我在第一行的值。我知道我需要更改 currentbal ID 的 ID 以使其唯一。我正在考虑做这样的事情:

<input name="currentbal(add the id here)" type="text" id="currentbal(add the id here)" class="input-mini"  />

使 currentbal ID 唯一,这样我的 currentbal ID 将分别变为 currentbal16 或 currentbal63。我的问题是如何编辑我的 js 函数,以便调用第 1 行

var currentbal = document.getElementById('currentbal').value; 

变成var currentbal = document.getElementById('currentbal16').value;

var currentbal = document.getElementById('currentbal').value; 当调用第 2 行时。基本上我猜有语法问题。任何指导将不胜感激。

最佳答案

var currentbal = document.getElementById('currentbal' + row_id).value;

您只需将数字 ID 添加到 'currentbal' 字符串的末尾即可创建一个新字符串。然后搜索该 ID。

关于php - 使用 javascript 时将函数值传递给输入 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14616827/

相关文章:

javascript - 导航栏始终显示在轮播图像后面

javascript - 在复选框上检查始终使用 Angular JS 获取警报中的第一行值

javascript - (Javascript- HTML Parser) 没有服务器语言可以用javascript解析html吗?

javascript - 页面更改时 Yii renderPartial 索引 View 重定向

javascript - AngularJS:防止验证隐藏的表单字段

php - 有没有办法初始化 PHPUnit 测试类模板?

php - 为什么 php 在引用一个不存在的变量时不报错?

php - 根据标签随机播放 YouTube 视频

php - 回显一行输出或关闭 php 标签与写出 html 代码之间有什么区别吗?

地址栏中的 javascript 字导致我的 apache Web 服务器瘫痪