首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >while循环中的Return语句莫名其妙地过早退出函数

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

Stack Overflow用户
提问于 2017-06-11 23:02:37
回答 1查看 47关注 0票数 0

我是Javascript的新手,但我尝试创建一个代码,访问Sharepoint-list。我有这段代码来访问列表"Mytasks",我想要获取select-tag中的项目。现在它只显示列表的第一项。我找不到问题所在。

代码语言:javascript
复制
// 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!');
    	}));

}

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2017-06-12 17:30:30

我解决了我的问题。我将行var list = document.getElementById("tstList");移到了while循环之前,并将返回移到了循环之后。所以现在一切都好了。谢谢你的回答。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44485258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档