简体   繁体   English

根据另一个attritube的结果设置选择标签值

[英]Set select tag value based on result of another attritube

I have a select element with options which contains groups: 我有一个带有包含组的选项的select元素:

<select id="my_SiteGroups" style="width:200px;">
      <option value="default" disabled="disabled">Select a group</option>
      <option value="Admin">Admin</option>
      <option value="Dev">Dev</option><option value="SSH-RAR">SSH-RAR</option> 
      <option value="Students">Students</option>
      <option value="test">test</option>
</select>

When I select a group I get an XML response with the selected groups's information: 当选择一个组时,我得到一个包含所选组信息的XML响应:

<GetGroupInfo xmlns=
   "http://schemas.microsoft.com/sharepoint/soap/directory/">
   <Group ID="3" Name="Group1" Description="Description" OwnerID="1" 
      OwnerIsUser="False" />
</GetGroupInfo>

I am trying to use the XML response to populate input elements with the groups information so I can use another function to update the groups info. 我试图使用XML响应来用组信息填充输入元素,以便可以使用另一个函数来更新组信息。 This is what it looks like: 看起来是这样的:

在此处输入图片说明

Based on the group ownerID from the groupInfo XML above, I'm trying to set the current group owner. 基于上述groupInfo XML中的组ownerID,我试图设置当前的组所有者。 The group owner select contains a list of all my users and is populated like this: 组所有者选择包含我所有用户的列表,并按以下方式填充:

<select id="groupOwner">
<option value="default">Select a group owner</option>
<option value="i:0#.w|itun\cbsadmin_comp.c" ownerid="70">cbsadmin</option>
. 
. 
. 
</select>

So from this I'm trying to get my code to look at the ownerId of the group from the returned XML, go through the groupOwner options in my groupOwner select tag and look for which option has the matching ownerID, then set my select tags value using that options value. 所以从这里我想让我的代码从返回的XML中查看组的ownerId,浏览groupOwner select标签中的groupOwner选项并查找具有匹配ownerID的选项,然后设置我的select tags值使用该选项值。

JS: JS:

function groupInfo(group){
  $().SPServices({
    operation: "GetGroupInfo",
    groupName: group,
    completefunc: function (xData, Status){
      var result = $(xData.responseXML);
      result.find('Group').each(function(){
        $('#groupID').val($(this).attr('ID'));
        $('#groupName').val($(this).attr('Name'));
        $('#groupDesc').val($(this).attr('Description'));
        $('#groupOwner').val($(this).attr('OwnerID'));
      });
    }
  });

HTML: HTML:

  <td>
    <label>Group ID: </label><input type="text" id="groupID" disabled="true"/></br>
    <label>Name: </label><input type="text" id="groupName" /></br>
    <label>Description: </label><input type="text" id="groupDesc" /></br>        
    <label>Owner: </label><select id="groupOwner"><option value="default">Select a Group Owner</option></select></br>
    <div class="listControl"><a onclick="updateGroupInfo()">Update Group Info</a></div>
  </td>

Try to use prop() like, 尝试像使用prop()一样,

// if owner id is based on indexing
$('#groupOwner').prop('selectedIndex',$(this).attr('OwnerID'));

Or try 尝试

$('#groupOwner option').removeAttr('selected');// remove selected attribute first
// then add selected attribute based on ownerId
$('#groupOwner option[value="'+$(this).attr('OwnerID')+'"]').attr('selected','selected');

Read removeAttr() 阅读removeAttr()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM