谁能发现在 3 个查询字符串参数中,2 个获取值和 1 个未定义的原因。
<li class="@ViewBag.ShowNext">@Html.RouteLink("Next »", "Search", new { page = @ViewBag.NextPage, q = @ViewBag.TextClean, Option = @ViewBag.Option }, new { id = "frmPageNext" })</li>
Javascript/jQuery
var vars = [], QStrings;
var q = document.URL.split('?')[1];
if (q != undefined) {
q = q.split('&');
for (var i = 0; i < q.length; i++) {
QStrings = q[i].split('='); vars.push(QStrings[1]);
vars[QStrings[0]] = QStrings[1];
}
}
$('#frmPageNext').live('click',function (e) {
e.preventDefault();
$.ajax({
url: '@Url.Action("Results")',
data: { "q": vars['q'], "page": vars['page'], "Option": vars['Option'] },
success: function (data) {
alert(vars['q']);
alert(vars['page']); //Returning undefined
alert(vars['Option']);
$("#dvSearchLeft").html(data).fadeIn(300);
}
});
});
-------------------------URL MVC-----------------
<a href="/Search-Results-For/Hotels/Web/2" id="frmPageNext">Next »</a>
The above would be <a href="/Search-Results-For/q=Hotels&Option=Web&Page=2
----------------当前代码--------------------------------
我曾尝试移动 javascript 以使代码正常工作,但所有 3 个参数都未定义
<script>
$('#frmPageNext').live('click',function (e) {
e.preventDefault();
var vars = [], QStrings;
var q = $('#frmPageNext').attr('href').split('?')[1];
if (q != undefined) {
q = q.split('&');
for (var i = 0; i < q.length; i++) {
QStrings = q[i].split('='); vars.push(QStrings[1]);
vars[QStrings[0]] = QStrings[1];
}
}
$.ajax({
url: '@Url.Action("Results")',
type: 'Get',
data: { "q": vars['q'], "Page": vars['page'], "Option": vars['Option'] },
dataType: 'html',
success: function (data) {
alert(vars['q']);
alert(vars['page']);
alert(vars['Option']);
$("#dvSearchLeft").html(data).fadeIn(300);
}
});
});
</script>