Javascript 循环遍历一个数组,并获取要传递给dom元素的值
如何获取数组中的选定选项值并将其传递给DOM元素?然后调用RESTAPI中的元素值来添加项。谢谢您的帮助 这是我的密码Javascript 循环遍历一个数组,并获取要传递给dom元素的值,javascript,Javascript,如何获取数组中的选定选项值并将其传递给DOM元素?然后调用RESTAPI中的元素值来添加项。谢谢您的帮助 这是我的密码 var myArray = ["#txtDepartment ","#txtLocation" ]; $.each(myArray, function(index, choices) { $(choices).change(function () { var selectedText =$(choices).find("option:selected").
var myArray = ["#txtDepartment ","#txtLocation" ];
$.each(myArray, function(index, choices) {
$(choices).change(function () {
var selectedText =$(choices).find("option:selected").text();
});
});
RESTAPI
function addListItem() {
var title = $("#txtTitle").val();
var siteUrl = _spPageContextInfo.webAbsoluteUrl;
var fullUrl = siteUrl + "/_api/web/lists/GetByTitle('ComputerEquipment')/items";
$.ajax({
url: fullUrl,
type: "POST",
data: JSON.stringify({
'__metadata': { 'type': 'SP.Data.ComputerEquipmentListItem' },
'Title': title,
'Department': ,
'Location':,
HTML
部门有什么问题:$(“#txtDepartment”).val()
?您对selectedText
@mplungjan的计划是什么?选项中可能添加了一些值
属性,在这种情况下,.val()
从属性中给出值,而不是从选项
标记中的文本中给出值。@JessieBear,问题仍然不清楚。你想做什么?您拥有所选下拉列表中的值。现在,当执行此addListItem()
函数时。您是否在选择器上绑定了某个事件?@AshishKumar-then'Department':$(“#txtDepartment选项:选中”).text()代码>谢谢@mplungjan!我只使用了这个,它更简单。'Department':$(“#txtDepartment选项:selected”).text();我没有使用数组。
<script type="text/javascript" <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="/sites/test/WebParts/Fruits/test2.js"></script>
<strong>Department:</strong>
<select id="txtDepartment">
<option value="HR">HR</option>
<option value="Finance">Finance</option>
<option value="Marketing">Marketing</option>
</select>
<br/>
<strong>Location:</strong>
<select id="txtLocation">
<option value="Singapore">Singapore</option>
<option value="USA">Indonesia</option>
<option value="UK">Malaysia</option>
<option value="Canada">Philippines</option>
</select>