根据其他属性的结果设置选择标记值

时间:2013-10-29 04:44:20

标签: javascript jquery html xml

我有一个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>

当我选择一个组时,我会得到一个包含所选组信息的XML响应:

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

我正在尝试使用XML响应来使用组信息填充输入元素,以便我可以使用其他函数来更新组信息。这就是它的样子:

enter image description here

基于上面groupInfo XML中的group ownerID,我正在尝试设置当前的组所有者。组所有者选择包含所有用户的列表,并填充如下:

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

因此,我试图让我的代码从返回的XML中查看该组的ownerId,查看我的groupOwner select标签中的groupOwner选项,并查找哪个选项具有匹配的ownerID,然后设置我的使用该选项值选择标签值。

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:

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

1 个答案:

答案 0 :(得分:0)

尝试使用prop()之类的,

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

尝试

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

阅读removeAttr()

相关问题