gpt4 book ai didi

javascript - 从 URL 加载变量,仅适用于数字?

转载 作者:搜寻专家 更新时间:2023-10-31 22:42:06 25 4
gpt4 key购买 nike

我的网址是 example.com/load_item.html?item=QB123

QB123 是字母和数字的随机组合,我们称之为 item_code。

我需要解析 URL 获取 item_code 并将其输入到页面上的表单中。

这是我所拥有的:

<div class="col_full">
<label for="item_code">Item Number:</label>
<input type="text" id="item_code1" name="item_code" class="form-control" required/>
</div>
<script type="text/javascript">
document.getElementById('item_code1').value=+window.location.search.substr(6) ;
</script>

如果 item_code 全部为数字(例如 12345),则有效。但是,当包含字母时,表单值将作为 NaN 提交。任何想法

最佳答案

为了连接字符串,加号应该在等号的左边:

document.getElementById('item_code1').value += window.location.search.substr(6) ;

否则,您会将 window.location.search.substr(6) 视为数字。

关于javascript - 从 URL 加载变量,仅适用于数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31818094/

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