循环遍历数组并获取值以在dom元素上传递它

时间:2015-08-28 09:28:46

标签: javascript

如何在数组中获取选定的选项值并将其传递给DOM元素?然后调用REST API中的元素值来添加items.Thanks的帮助。

这是我的代码。

var myArray = ["#txtDepartment ","#txtLocation" ];
$.each(myArray, function(index, choices) {
    $(choices).change(function () {
       var selectedText =$(choices).find("option:selected").text();     
    });
});

REST API

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

<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>

0 个答案:

没有答案