繁体   English   中英

如何向Jquery UI Multiselect添加新选项

[英]How can I add new options to Jquery UI Multiselect

我已经尝试了好一阵子,但似乎无法为multiselect添加更多选项。

即使当我尝试添加一条硬编码的信息时,它也不会被添加。 (Multiselect和JQuery的头在另一个文件中)。

您可以在此处查看运行中的链接: http : //www.fakegaming.eu/GoT/index.php? act=selectteam& matchid=3

<?php
$matches = new Matches();
$user = new User();
$id = $_GET['matchid'];
$matchinfo = $matches->getmatch($id);
$matchplayers = $matches->getmatchplayers($id);
?>
<script type="text/javascript">
    $(function(){
        $.localise('ui-multiselect', {/*language: 'en',*/ path: 'script/locale/'});
        $(".multiselect").multiselect();

        $("#add").click(function() {
          $('#players').multiselect('addOptions', 'Test=123');
          alert("Handler for .click() called.");
        });
    });
</script>
Kies hier 10 spelers die mee doen aan de match.<br /> <br />

<form action='index.php?act=createteam' method='post'>
    <select id="players" class="multiselect" multiple="multiple" name="players[]">
    <?php
        foreach($matchplayers as $matchplayer){
            $userinfo = $user->getuserbyid($matchplayer['user_id']);
            if($matchplayer['type'] == 0 OR $matchplayer['type'] == 2){
                $name = $userinfo['EU_username'];
                ?>
                <option value="<?= $name ?>"><?= $name ?></option>
                <?php
            }
        }

    ?>
    </select>
    <input name='name' type='text' id='name' />
    <input type='button' id="add" name="add" value="Toevoegen" />
    <input name="submit" value="Maak team" type="submit" />
</form>

我可能只是在做一些可怕的错误,但是我只想要一个好的多选并为其添加一些名称。

您说使用的是jQuery,但我却很少使用它...这是将<OPTION>添加到您的multiselect中的“ jQuery方式”

$('#players').append($('<option></option>').attr('value', '123').text('345'));

要选择一个属性:

$('#players option[value="123"]').attr('selected', true);

取消选择属性:

$('#players option[value="123"]').removeAttr('selected');

删除选项

$('#players option[value="123"]').remove();

** 编辑 **

(我没有查看您的参考链接。我应该是最好的选择,我是此小部件的原始作者之一!)

我和Michael Aufreiter共同开发的此小部件的下一版本实现了您所需的更多功能。 在某些配置下,它有点不稳定,但在基本(默认)设置下应该足够稳定。

您可以在此处找到它,也可以通过以下选项访问值:

 $('#players').multiselect('addOptions', '123=456'); // value=123, text=456

抱歉,尚未更新API文档以对此进行记录。 实际上,此小部件得到其他贡献者的支持,我们中的任何一位(原始作者)都维护此版本。

希望这可以帮助。

如果您想将玩家提交到index.php?act=createteam操作中,请确保在发送请求之前已选择玩家。

<script>
    function selectPlayers() {
        var opts = document.getElementById("players").options;
        for (var i = 0; i < opts.length; i++) {
            opts[i].selected = "selected";
        }
        return true;
    }
</script>

<form action='index.php?act=createteam' method='post' onsubmit='selectPlayers()'>

暂无
暂无

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

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