【问题标题】:Set select tag value based on result of another attritube根据另一个属性的结果设置选择标签值
【发布时间】:2013-11-08 02:41:20
【问题描述】:

我有一个带有选项的选择元素,其中包含组:

<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 响应来使用组信息填充输入元素,以便我可以使用另一个函数来更新组信息。这是它的样子:

基于上述 groupInfo XML 中的组 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 选择标记中的 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>

【问题讨论】:

    标签: javascript jquery html xml


    【解决方案1】:

    尽量用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()

    【讨论】:

    • 您能解释一下这是如何解决我的问题的吗?我不明白。我看不到解决方案如何将从 xml 返回的所有者 ID 与我的列表项中的所有者 ID 进行比较以设置匹配列表项的值。
    • 首先删除selected attribute,然后使用option[value="'+$(this).attr('OwnerID')+'"] 选择具有value=new owner idoption,参见api.jquery.com/attribute-equals-selector
    • 我想你错过了理解。看看我的 html,我没有尝试将值设置为所有者 ID
    • @Batman 我的代码没有设置value,这里我用它作为selector 其中value matches your owner id
    • 好的,我看到了问题。您正在寻找具有相同 ownerid 但在列表项 ownerid 中是它自己的自定义属性的选项值。如果您更改 option[ownerid="..."] 它工作正常。谢谢
    猜你喜欢
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    相关资源
    最近更新 更多