在初始选择更改时,jquery不会更新/渲染视图中的html
On initial select change, jquery does not update/render html in the view
选择更改后,我的视图似乎落后了一步。我有一个使用getJSON请求填充的选择/下拉列表。在初始选择之后,我在fiddler中验证请求成功,但我的视图没有更新。疯狂的是,当我做出另一个选择时,视图就会用之前的数据更新,并以这种方式继续。我错过了什么?
这是我的HTML:<div id="ClientSection">
<p>
@Html.Label("clientId", "Client")
@Html.DropDownListFor(x => x.PrimaryClient, Enumerable.Empty<SelectListItem>(),
"Choose Client", new {id = "clientId"})
</p>
<table id="clientLocationsTable">
<thead>
<tr>
<th>Region</th>
<th>Location</th>
<th>Address</th>
<th>Suite</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Phone #</th>
<th>Email</th>
<th>Contact</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
和我的JavaScript:
@section scripts
{
<script>
$(document).ready(function () {
// populate main client dropdown
$(function() {
$.getJSON("/api/client/getclients/", function(data) {
$.each(data, function (index, clientObj) {
$("#clientId").append(
$("<option/>").attr("value", clientObj.Id)
.text(clientObj.CompanyName)
);
});
});
});
// create new array
var otherClientLocations = new Array();
$("#clientId").change(function () {
// clear table body
$("#clientLocationsTable > tbody").empty();
// create new array
var clientList = new Array();
// set the id
var primaryId = $("#clientId").val();
$.getJSON("/api/client/getclientotherlocations/" + primaryId, function (data) {
// populate otherClientLocations Array
$.each(data, function(key, val) {
clientList.push(val);
});
otherClientLocations = clientList;
});
// create rows if needed
if(otherClientLocations.length > 0) {
$.each(otherClientLocations, function(key, val) {
$("#clientLocationsTable tbody")
.append("<tr><td>" + val.CompanyRegion +
"</td><td>" + val.CompanyLocationCode + "</td>"
+ "<td>" + val.CompanyAddress + "</td>" + "<td>" +
val.CompanySuite + "</td><td>" + val.CompanyCity +
"</td><td>" + val.CompanyState + "</td><td>" +
val.CompanyZipCode + "</td><td>" + val.CompanyPhoneNumber
+ "</td><td>" + val.CompanyEmail + "</td><td>" +
val.CompanyContactFn + " " + val.CompanyContactLn +
"</td>" + "</tr>");
});
}
});
});
</script>
}
您没有考虑到json正在异步获取的事实。在从服务器返回json之前更新dom。
试题:
$(document).ready(function () {
// populate main client dropdown
$(function() {
$.getJSON("/api/client/getclients/", function(data) {
$.each(data, function (index, clientObj) {
$("#clientId").append(
$("<option/>").attr("value", clientObj.Id)
.text(clientObj.CompanyName)
);
});
});
});
// create new array
var otherClientLocations = new Array();
$("#clientId").change(function () {
// clear table body
$("#clientLocationsTable > tbody").empty();
// create new array
var clientList = new Array();
// set the id
var primaryId = $("#clientId").val();
$.getJSON("/api/client/getclientotherlocations/" + primaryId, function (data) {
// populate otherClientLocations Array
$.each(data, function(key, val) {
clientList.push(val);
});
otherClientLocations = clientList;
// create rows if needed (the section below has now been moved inside the callback
if(otherClientLocations.length > 0) {
$.each(otherClientLocations, function(key, val) {
$("#clientLocationsTable tbody")
.append("<tr><td>" + val.CompanyRegion +
"</td><td>" + val.CompanyLocationCode + "</td>"
+ "<td>" + val.CompanyAddress + "</td>" + "<td>" +
val.CompanySuite + "</td><td>" + val.CompanyCity +
"</td><td>" + val.CompanyState + "</td><td>" +
val.CompanyZipCode + "</td><td>" + val.CompanyPhoneNumber
+ "</td><td>" + val.CompanyEmail + "</td><td>" +
val.CompanyContactFn + " " + val.CompanyContactLn +
"</td>" + "</tr>");
});
}
});
});
});
澄清:当http请求正在进行时,javascript仍在并发执行。你的版本是这样的:
$.getJSON("/api/client/getclientotherlocations/" + primaryId, function (data) {
// update array AFTER request is complete
});
// update dom based on value of array while request is still in progress
我移动了一些括号,现在是:
$.getJSON("/api/client/getclientotherlocations/" + primaryId, function (data) {
// update array AFTER request is complete
// then update dom based on new version of array
});
相关文章:
- 在控制器之间切换,可以't更新视图
- 拖动&删除Fullcalendar.io资源-更新视图
- 由控制器调用的服务更改时,Angular未更新视图
- 如何在Knockout.js中选中复选框时更新视图模型及其依赖项
- 当视图在AngularJS中返回时,我如何获得异步服务调用来更新视图
- 角度:当输入更改时更新视图 + .this/$scope
- 数据库中的数据更改时更新视图
- ng单击需要两次才能更新视图
- 范围变量更新,但不更新视图
- 在jquery中更新视图模型
- 在Meteor.js中通过同一路线内的锚点标签更新视图
- 更新视图和控制器之间的角度重复数据值的最佳方式是什么
- AngularJS 服务不会更新视图
- AngularJS 模式单例在服务中未更新视图
- 角度 JS 脚本未更新视图
- AngularJS 在$rootScope更改时更新视图
- 如果外部应用程序更改了持久模型(服务器数据库),AngularJS 是否可以自动更新视图
- AngularJS$scope更改不更新视图
- 如何在不使用映射的情况下更新视图模型
- 用于引导程序选择的挖空自定义绑定,不更新视图模型“选定类别/选定类别”