while 循环中的 return 语句莫名其妙地过早退出函数



我是Javascript的新手,但我尝试创建一个代码,可以访问Sharepoint列表。我有此代码可以访问列表"Mytasks",并且我想在选择标签中获取项目。现在它只显示列表的第一项。我找不到问题。

// load all necessary sharepoint javascript libaries
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
	// load the sharepoint list.
	loadSharepointList();
});
// loads the sharepoint list
function loadSharepointList() {
	// create the sharepoint content.
	var context = SP.ClientContext.get_current();
	// get the list by the title.
	var list = context.get_web().get_lists().getByTitle('myTasks');
	// create the query.
	var caml = new SP.CamlQuery();
	caml.set_viewXml("<Query><Where><IsNotNull><FieldRef Name='Title' /></IsNotNull></Where></Query>");
	// get the list items asynchronously
	var listItems = list.getItems(caml);
	context.load(listItems, 'Include(Title)');
	context.executeQueryAsync(
    	// success delegate
    	Function.createDelegate(this, function () {
    	
    		// loop through the items.
    		var listEnumerator = listItems.getEnumerator();
    		while (listEnumerator.moveNext()) {
    			// get the current list item.
    			var listItem = listEnumerator.get_current();
    			// get the field value.
    			var fieldValue = listItem.get_item('Title');
				
    			var list = document.getElementById("tstList");
    			var option = document.createElement("option");
    			option.text = fieldValue;
    			list.add(option);
    		//	console.log(fieldValue);
    			return "<option>" + fieldValue + "</option>";
    			
    		}
     	}),
    	// error delegate
    	Function.createDelegate(this, function () {
    		alert('Error fetching data from Sharepoint!');
    	}));
}

知道吗?

我解决了我的问题。 我移动了行 var list = document.getElementById("tstList"(; 在 while-loop 和 return 之前,我在循环之后移动。所以现在很好。感谢您的回答。

最新更新