iT邦幫忙

0

使用javascript動態代入資訊至下拉選單(drop-down menu)

HTML5:

<select id="testoption" name="testoption" style="cursor:pointer; width:150px" class="btn btn-default dropdown-toggle" onchange="select_testoption()">
</select>

JavaScript:

$(document).ready(function () {
	test="iamtest";

	$.ajaxSetup({
		data: {
			csrfmiddlewaretoken: '{{ csrf_token }}'
		},
	});

	$.ajax({
		url:"**views.py的function**",
		type:'POST',
		data:{test:test},
		success: function (result) {
			console.log(result); //["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
			innerstart = '<option value="請選擇">請選擇</option>';
			for(var i=0;i<result.length;i++){
				inner=inner+'<option value='+result[i]+'>'+result[i]+'</option>';
			}
			document.getElementById("testoption").innerHTML=innerstart+inner;
		}
	});
});

參考:https://ithelp.ithome.com.tw/articles/10193376


尚未有邦友留言

立即登入留言