1

Here is my code i tried to print the selected value from selected box.But its printing only the first value of selected item.I don't know how to get the length of selected value.any one help me for this problem

<html>
    <head>
        <title>jQuery Dropdown CheckList</title>
        <link rel="stylesheet" type="text/css" href="jquery-ui-1.8.13.custom.css">
        <link rel="stylesheet" type="text/css" href="ui.dropdownchecklist.themeroller.css">
        <style>
    table td { vertical-align: top }
    dd { padding-bottom: 15px }
        </style>

        <script type="text/javascript" src="jquery-1.6.1.min.js"></script>
        <script type="text/javascript" src="jquery-ui-1.8.13.custom.min.js"></script>
        <script type="text/javascript" src="ui.dropdownchecklist-1.4-min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
              $("#s10").dropdownchecklist( { forceMultiple: true
    , onComplete: function(selector) {
        var values = "";
        for( i=0; i < selector.options.length; i++ )
        {
            if (selector.options[i].selected && (selector.options[i].value != "")) 
            {
                if ( values != "" ) 
                  values += ";";
                values += selector.options[i].value;            
            }

        }
        alert(values);
        } 
    , onItemClick: function(checkbox, selector)
       {
         var justChecked = checkbox.prop("checked");
         var checkCount = (justChecked) ? 1 : -1;
         for( i = 0; i < selector.options.length; i++ ){
            if (selector.options[i].selected )
              checkCount += 1;
        }


       }
      });
    });
        </script>  
    </head>
    <body>
    <div id="content">
      <table>
          <tr>
            <td>
                <select id="s10">
                    <option value=""></option>
                    <option value='a'>Alice</option>
                    <option value='b'>Bob</option>
                    <option value='c'>Christian</option>
                    <option value='d'>Daniel</option>
                    <option value='e'>Elizabeth</option>
                </select>
             </td>
           </tr>
        </table>  
    </div>
    </body>
    </html>

for taking the length of checkbox selected item I did this but i don't know it only giving 1 as alert message

onItemClick: function (checkbox, selector) 
                 {
                   $("input:checked").each(function()
                   {        
                         alert($(this).val().length);    
                   });         
                 } });  
4

3 に答える 3

1

選択したオプションを次のように取得します:selected

$('#s10 option:selected').val();

私が言いたかったのはこれです: http://jsfiddle.net/T4UDu/

$('#s10').change(function(){
    alert($('option:selected',this).val());
});
于 2013-02-16T04:42:32.927 に答える
1

これを試してください、うまくいくことを願っています

<select id="s10" multiple="multiple">

    <script type="text/javascript">
        $(document).ready(function() {
        var foo = []; 
        $('#s10:selected').each(function(i, selected){ 
          foo[i] = $(selected).text(); 
        });
      });
   </script>  
于 2013-02-16T04:43:01.460 に答える
0
$('#checkboxId').change(function(){
    alert($('option:selected',this).val());
});
于 2013-02-16T12:26:16.007 に答える