我在从 url 获取值时遇到问题,我有一个显示模式的链接 就像我一样
<a href='#modal?userId=<%=resultSet.getInt("drid")%>' class="button-border toggleModal">Apportionment</a>
上面的链接打开模式如下
<div id="modal" class="modal">
<header>
<h2>Appointment Form</h2>
</header>
<form action="addappoinment.jsp" method="post">
<input type="hidden" id="docId" name="docid" value=""><br>
<input type="text" required><br>
<input type="text" required><br>
<input type="text" required><br>
<input type="text" required><br>
<textarea rows="4" cols="21"></textarea><br>
<button class="button-border button-success" type="submit">
Done</button>
<button class="button-border button-error pull-right toggleModal">
Cancel</button>
</form>
</div>
现在我有一个像
这样的网址http://localhost:8080/app/#modal?userId=1
现在我想使用 javascript 获取 userId
参数的值
<script type="text/javascript">
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var param = getParameterByName("userId");
var userId = document.getElementById("docId");
userId.value = param;
</script>
但无法从http://localhost:8080/app/#modal?userId=1
最佳答案
#modal?userId=1
查询字符串(发送到服务器)必须位于片段标识符(完全由客户端处理)之前。
即
?userId=1#modal
如果您不想将该数据发送到服务器,则需要读取片段标识符 (location.hash
) 并将其拆分到 ?
以获得您需要的作品。
关于javascript - 如何使用 javascript 或 jquery 从 url 参数获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34656824/