gpt4 book ai didi

javascript - 将 JavaScript 参数中的字符串转换为整数

转载 作者:行者123 更新时间:2023-11-28 18:51:50 24 4
gpt4 key购买 nike

我有一个 JavaScript 函数,它接受两个参数:第一个参数是给定 html 分区的 ID,第二个参数是一行 resultSet 的内容(从数据库返回)。

    function displayItem(item_ID, content)
{
alert(typeof(item_ID)); // this one returns a String
el = document.getElementById(item_ID);
alert(el);

dispArea=document.getElementById("individual");
dispArea.innerHTML=content;

}

当我尝试在我的链接之一中调用该函数时,问题就出现了:

     <ul id="principle_items">
<!-- <%int x=0; %> !-->
<c:set value ="0" var="x" />
<c:forEach var="row" items="${result.rows}">
<c:set value ="${x+1}" var="x" />
<!-- <% out.println("the value of X is " + x +"\n");%> !-->

<li><a id="${x}" href="#" onclick="displayItem(this.id,'${result.rows[8].PRNCPL_NAME}');"><c:out value="${row.PRNCPL_NAME}"/></a></li>
</c:forEach>
</ul>

我希望索引是 this.id 的整数值,而不是选择 reslut.rows[8](我所有的 ID 都是“1”、“2”、“3”...)
onclick="displayItem(this.id,'${result.rows[8].PRNCPL_NAME}');"

当我尝试时 onclick="displayItem(this.id,'${result.rows[this.id].PRNCPL_NAME}');"它显然不起作用,因为 this.id 是一个字符串。我知道我可以在 Java 中使用 parseInt,但我不知道如何在同一个语句中执行 if 操作。

最佳答案

使用parseInt函数

alert(typeof(parseInt(item_ID)));
//should be "number"

演示

var item_ID = "7";
alert(typeof(parseInt(item_ID)));

关于javascript - 将 JavaScript 参数中的字符串转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34378190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com