简体   繁体   English

如何在jQuery中使用多重选择?

[英]How to use multiple select with jQuery?

I need to submit a form with multiple values 我需要提交具有多个值的表格

<form onsubmit="return false;">
<?php

echo "<select id=\"uname\" name=\"uname[]\" multiple>\n";

$sql = "SELECT * FROM users ORDER BY surname ASC";
$res = $db->query($sql);
while($row = $res->fetch()){
        $strA = $row["uname"];
        $strB = $row["givenname"];
        $strC = $row["surname"];
        echo "<option value=\"$strA\">$strC, $strB</option>\n";
}
echo "</select>\n";
echo "<input type='button' name='endreBruker' value='OK'   onclick='javascript:".$_POST['edit']."(this.form)'/>";
?>

and pass the POST values through this to newNote.php 并通过它传递POST值到newNote.php

function newNote(form) {
    $('#main').load ('newNote.php', {'uname[]' : form.uname.value} );
}

Problem is I only receive an array with a single value. 问题是我只收到一个具有单个值的数组。

Check out jQuery's serialize() . 查看jQuery的serialize() You could then write something like this: 然后,您可以编写如下内容:

function newNote(form) {
    $('#main').load ('newNote.php', $(form).serialize() );
}

You may try something like that: 您可以尝试类似的方法:

<select name="theselect" onchange="this.selectedIndex = 1;">  
<option value="Red">Red</option>  
<option value="Green" selected="selected">Green</option>  
<option value="Blue">Blue</option>  
</select>

Green color always been selected, but user may see other color, but don`t pick them 始终选择绿色,但用户可能会看到其他颜色,但不要选择它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM