Jquery取得多选框的val()和text()示例

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
<title>Playground</title>  
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script>  
<script type="text/javascript">  
$(function(){  
//  
})  
</script>  
</head>  
<body>  
<select id="s1" style="width:100px" size="5" multiple="true">    
        <option value="1">number 1</option>    
        <option value="2" selected>number 2</option>    
        <option value="3">number 3</option>    
        <option value="4" selected>number 4</option>    
        <option value="5">number 5</option>    
        <option value="6">number 6</option>    
        <option value="7">number 7</option>    
        <option value="8">number 8</option>    
        <option value="9">number 9</option>    
        <option value="10">number 10</option>    
        <option value="11">number 11</option>    
    </select>  
    <p>if we choose number 2 and number 4</p>  
    <button onclick="alert($('#s1').val())">val()</button>  
    return '2,4'<br />
    <button onclick="$('#s1 :selected').each(function(){alert($(this).val())})">val() one by one</button>    
    return '2'  then  
    return '4'<br /><br />

    <button onclick="alert($('#s1 :selected').text())">text()</button>  
    return 'number 2number 4'<br />
    <button onclick="$('#s1 :selected').each(function(){alert($(this).text())})">text() one by one</button>    
    return 'number 2'  then  
    return 'number 4'<br />
</body>  
</html>  

你可能感兴趣的:(html,jquery,XHTML)