我需要在页面加载期间调用GetAllProperties()函数,而不是在页面完全加载后调用GetAllProperties()函数。
我试过window.onload = GetAllProperties;但它没有用
我的代码如下所示:
<script type="text/javascript">
window.onload = GetAllProperties;
function GetAllProperties() {
$.ajax({
cache: false,
url: '/Home/GetAllProperties',
type: 'GET',
contentType: "application/json; charset=utf-8",
success: function (response) {
if (response.list.length > 0) {
console.log(response.list)
var $data = $('<table id="mytable" class="table table-striped"> </table>');
var header = "<thead><tr><th>Property Name</th><th>Edit</th></tr></thead>";
$data.append(header);
$.each(response.list, function (i, row) {
var $row = $('<tr/>');
$row.append($('<td/>').html(row.PropertyName));
$hidden = $(' <input type="hidden" name="hid" value= "' + row.PropertyId + '">');
$row.append($hidden);
$editButton = $("<button class='editbtn' id='mybtn'>Edit</button>");
$row.append($editButton);
$deleteButton = $("<button class='deletebtn' id='delbtn'>Delete</button>");
$row.append($deleteButton);
$data.append($row);
});
$("#MyDiv").empty();
$("#MyDiv").append($data);
}
else {
}
},
error: function (r) {
alert('Error! Please try again.' + r.responseText);
console.log(r);
}
});
}
</script>
答案 0 :(得分:2)
您正在使用jQuery,为什么不将事件监听器与window
绑定在一起?
$(window).on("load", GetAllProperties);
function GetAllProperties() {
alert("loaded");
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
但它甚至没有工作:
window.onload = GetAllProperties;
function GetAllProperties() {
alert("loaded");
}
&#13;