如何获取动态id的值

weixin_42562620 2019-02-15 11:37:47
<table border="1" width="80%" align="center" style="border-collapse:separate; border-spacing:15px;">
<tr><th >方法名称</th><th>函数参数</th><th>预期结果</th><th>实际结果</th><th>操作</th></tr>
#foreach($function in $functionEnum)
<tr>
<td>$function.code $function.name<input type="hidden" value="$function.code" id="td1_$function.code"/></td>
<td><input type="text" value="" id="td2_$function.code"/></td>
<td><span id="fwResult"></span></td>
<td><span id="td3$function.code"></span></td>
<td align="center" valign="middle"><input type="button" value="提交" onclick="ljlfunction()"/></td>
</tr>
</table>

<script type="text/javascript">

function ljlfunction() {
var params = document.getElementById("td2_$function.code").value;
alert(params);
var x = document.getElementById("td1_$function.code").value;
alert(x);
</script>

这里我无法获取id为“td2_$function.code”和“td1_$function.code”的值,求解答(说一下解决思路),谢谢。
...全文
276 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_42562620 2019-02-15
  • 打赏
  • 举报
回复
有没有人帮一下忙,有点急!!
weixin_42562620 2019-02-15
  • 打赏
  • 举报
回复
非常感谢您,我的问题解决了
天际的海浪 2019-02-15
  • 打赏
  • 举报
回复
你的html代码部分是个模板,在模板中$function.code是变量,在生成页面时会被替换成实际的内容。 但是在js代码中的$function.code是不会被替换的。 你可以在模板中对ljlfunction()函数传参
<table border="1" width="80%" align="center" style="border-collapse:separate; border-spacing:15px;">
        <tr><th >方法名称</th><th>函数参数</th><th>预期结果</th><th>实际结果</th><th>操作</th></tr>
        #foreach($function in $functionEnum)
            <tr>
                <td>$function.code $function.name<input type="hidden" value="$function.code" id="td1_$function.code"/></td>
                <td><input type="text" value="" id="td2_$function.code"/></td>
                <td><span id="fwResult"></span></td>
                <td><span id="td3$function.code"></span></td>
                <td align="center" valign="middle"><input type="button" value="提交" onclick="ljlfunction('td2_$function.code','td1_$function.code')"/></td>
</tr>
</table>

 <script type="text/javascript">

                function ljlfunction(id1,id2) {
                    var params = document.getElementById(id1).value;
                    alert(params);
                    var x = document.getElementById(id2).value;
                    alert(x);
            }
</script>
weixin_42562620 2019-02-15
  • 打赏
  • 举报
回复
呃,您确实很仔细但没有解决我的问题,你说的那个错误是我复制黏贴的时候粗心忘加了,在我idea中代码是完整的,您还能提供解决方法吗?
  • 打赏
  • 举报
回复

<script type="text/javascript">

function ljlfunction() {
var params = document.getElementById("td2_$function.code").value;
alert(params);
var x = document.getElementById("td1_$function.code").value;
alert(x);
}
//少了}符号
</script>

87,920

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧