.text()给出元素中的所有文本元素

时间:2013-10-01 05:30:18

标签: javascript jquery jquery-selectors

下面的函数假设给我一个警告,询问我是否确定要删除所选用户。但是,警报会为我提供所有用户的列表,而不是所选用户的列表。

var user, group, strHTMLSiteUsers, strHTMLSiteGroups, strHTMLAvailable, strHTMLAssigned, arrOptionsAssigned, arrGroups, arrUsers, intOpts, booMatch, booErr;

$(document).ready(function(){ 
    user = $('#my_SiteUsers');
    group = $('#my_SiteGroups');
    groupsAssigned = $("#my_SPGroupsAssigned").html("Cats");
    groupAvailable = $("#my_SPGroupsAvailable").html("Cats");
    userAssigned = $("#my_SPUsersAssigned").html("Cats");
    userAvailable = $("#my_SPUsersAvailable").html("Cats");

    $("button").click(function() { return false; });

    populateUsers();
    populateGroups();
});

 if(user.val()!= null){
    var removeConfirm = confirm("You are about to delete "+user.text()+" from SharePoint. Are you sure?");
    if(removeConfirm){
      $().SPServices({
        operation:"RemoveUserFromWeb",
        userLoginName: user.val(),
        async:true,
        completefunc: function (xData,Status){
            alert(user.text()+ " deleted succesfully");
        }
      });
    }
  }else{
    alert("Please select a user");
  }
}

HTML:

 <select id="my_SiteUsers" style="width:200px;" onchange="RefreshGroupLists()">
        <option value='default' disabled="disabled">Select a user</option>
      </select>

4 个答案:

答案 0 :(得分:1)

获取所选用户文字。

var user_text = $('#my_SiteUsers option:selected').text();


   var user_text = user.find("option:selected").text();

答案 1 :(得分:1)

仅获取选定的用户

user.find('option:selected').text()

答案 2 :(得分:1)

您可以使用

user.find(':selected').text();

在您的代码中,它可能是

alert(user.find(':selected').text() + " deleted succesfully");

DEMO.

答案 3 :(得分:1)

You can use if you are using select box:

var userText = $('#my_SiteUsers option:selected').text();
alert(userText);

var userText = $('#my_SiteUsers').find(':selected').text();
alert(userText);