【问题标题】:convert string to Integer in a javascript parameter在javascript参数中将字符串转换为整数
【发布时间】:2016-03-26 11:52:57
【问题描述】:

我有一个 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,但我不知道如果在同一个语句中该怎么做。

【问题讨论】:

    标签: javascript html jsp parsing type-conversion


    【解决方案1】:

    使用parseInt函数

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

    演示

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

    【讨论】:

    • 如果我在函数调用中使用它,我得到错误:在指定的标签库中找不到函数“parseInt”
    • 试试window.parseInt
    • 请不要:我想在函数调用过程中转换它,而不是在JS函数体中。
    • 尝试使用函数体中的window.parseInt(item_ID)
    • onclick="displayItem(this.id,'${result.rows[window.parseInt(this.id)].PRNCPL_NAME}' 似乎不起作用。
    【解决方案2】:

    认为问题在于解析 JSP 和 JavaScript。它们在不同的时间完成。所以你不能在 JSP 解析时运行 JavaScript。

    所以你的 ID 只是 ${x},你能不能像这样把 x 放到你的代码中...

    onclick="displayItem(this.id,'${result.rows[x].PRNCPL_NAME}');"
    

    或者类似的东西?

    【讨论】:

      【解决方案3】:

      您会想要使用parseInt,如Tareq Mahmood's answer 中所述,但parseInt 函数也接受一个基数,这会导致始终使用该base 防止读取某些数字(例如 08)时出现潜在错误。

      来自 MDN:

      基数

      一个介于 2 到 36 之间的整数,表示基数(在 上述字符串的数学数字系统)。指定 10为人类常用的十进制数字系统。总是 指定此参数以消除读者混淆并保证 可预测的行为。不同的实现产生不同的 未指定基数时的结果,通常将值默认为 10.

      你的函数变成这样:

      function displayItem(item_ID, content) {
          var item_ID = parseInt(item_ID, 10);  
          alert(typeof(item_ID)); // this one returns a String
          el = document.getElementById(item_ID);
          alert(el);
      
          dispArea = document.getElementById("individual");
          dispArea.innerHTML=content; 
      
      }
      

      如果您必须使用十进制数字,那么 parseFloat 的兄弟可能会帮助您。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 2011-05-09
        • 1970-01-01
        • 2014-12-24
        • 2016-01-03
        • 2023-03-29
        • 1970-01-01
        相关资源
        最近更新 更多