开发者

Can't get selected values from a multiple select list to insert into other select list

I'm a javasc开发者_如何学JAVAript newbie so please, any help would be appreciate. I've been trying to get selected values from a drop down list on my page in order to swap them . I've tried looking for solutions online but I'm stumped. For some reason, s1[i].selected doesn't work at all. Here's my javascript code:

<script type="text/javascript">
    function swaprows(s1, s2)
    {
        var items = "";
        var len = s1.length;
        for(i=0;i<len;i++)
        {
            if(s1[i].selected)
            {

            }
        }
    }       
</script>

The thing is I'm generating these listboxes on the fly using php, they're not hard coded into my HTML. Here's the code I use to create them and call the javascript function:

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

    <ul>
    <?php
    do
    {
        echo "<li>";
        echo "<h2 class=\"expand\" >Choose the sizes for " . $product_array['Product Name'] . "\n</h2>";
        echo "<div class=\"collapse\">";
        echo "<table border=\"0\">";
        echo "<tr>";
        echo "<td rowspan=\"2\"><select size=\"8\" multiple=\"true\" name=\"selectsizes[]\" id=\"selectsizes\">\n";
        for($i=0; $i < sizeof($sizes_data); $i++)
        {
            echo "<option value=" . $sizes_data[$i]['Size ID'] . ">" . $sizes_data[$i]['Size Name'] . "</option>\n";
        }
        echo "</select></td>\n";

        echo "<td valign=\"top\"><input type = \"button\" OnClick=\"swaprows(this.form.selectsizes, this.form.selectedsizes);\" value=\"-->\" /></td>";

        echo "<td rowspan=\"2\"><select multiple=\"true\" name=\"selectedsizes[]\" id=\"selectedsizes\">\n";
        echo "</select></td>\n";

        echo "<td rowspan=\"2\" valign=\"bottom\"><input type=\"submit\" id=\"btnSubmit\" name=\"SaveSizes[" . $product_array['Product ID'] . "]\" value=\"Save\" /></td>";

        echo "</tr><tr>";

        echo "<td valign=\"top\"><input type = \"button\" OnClick=\"swaprows(this.form.selectedsizes, this.form.selectsizes);\" value=\"<--\" /></td>";         

        echo "</tr>";

        echo "</table>";

        echo "</li>";
    }while($product_array = mysql_fetch_assoc($product_result));    

    ?>
    </ul>
    </form>

Because I'm doing a post action with the submit buttons I need to name my lists as selectsizes[] and selectedsizes[] respectively so that I can then pass their selected values via the post variable. Can anyone help me out here?


I'm pretty sure you don't need javascript for this.. just the [] in the name of the field. then on the other side php will auto make the array in the post variable of the selected fields.

$sizes = $_POST['select_sizes'];
$quantity = sizeof($sizes);
$inc = 0;
while ($inc < $quantity) {
    echo "User selected:".$sizes[$inc];
    $inc++;
}


Try if(s1[i].selected == true)

or

var sPool = s1[i];
if(sPool.selected == true)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜