[英]JavaScript stop working after AJAX call
My javascript is getting locked after ajax call. 我的JavaScript在ajax调用后被锁定。
When the user press enter then i call my c# method to search the city that the user typed, and then show the temperature, but when the search ends, the javascript stops working. 当用户按下Enter键时,我调用c#方法搜索用户键入的城市,然后显示温度,但是当搜索结束时,javascript停止工作。 And i found out that the error is on these lines: var div = document.getElementById('rb-grid');
我发现错误在以下几行:var div = document.getElementById('rb-grid'); div.innerHTML = resp.responseText + div.innerHTML;
div.innerHTML = resp.responseText + div.innerHTML;
code: 码:
$(document).ready(function () {
$('#search-bar').keyup(function (event) {
if (event.keyCode == 13) {
myFunction();
}
});
function myFunction() {
var city = $('#search-bar').val();
$.ajax({
url: '@Url.Action("getWeatherSearch", "Index")',
data: { city: city },
async: false,
complete: function (resp) {
var div = document.getElementById('rb-grid');
div.innerHTML = resp.responseText + div.innerHTML;
Boxgrid.init();
}
});
} });
HTML: HTML:
<div align="center" class="div-search-bar">
@Html.TextBox("search-bar", "", new { @class = "search-bar", placeholder = "search" })
</div>
Try the following and see if it works for you: 请尝试以下操作,看看它是否适合您:
$(function () {
var $searchbar = $('#search-bar'),
$grid = $('#rb-grid');
if ($grid.length) {
$searchbar.on('keyup', function (event) {
if (event.keyCode == 13) {
myFunction();
}
});
function myFunction() {
var city = $searchbar.val();
$.ajax({
url: $.grid.data('weather-url'),
data: { city: city }
})
.done(function (resp) {
$grid.html(resp.responseText + $grid.html());
Boxgrid.init();
}
});
}
}
});
Add the following as a data attribute somewhere in your html, probably on your grid: 将以下内容作为数据属性添加到html中的某处(可能在网格中):
<div id='rb-grid' data-weather-url='@Url.Action("getWeatherSearch", "Index")'></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.