jquery - 如何定义由随机数选择的 jQuery selectables?

标签 jquery css selection

如何定义由随机数选择的 jQuery 可选项? 到现在为止,如果我单击它们中的每一个,可选择项就会被选中。 但我想让这些随机自动选择。

我的代码是这样的:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">

<style type="text/css">
#selectable .ui-selecting { background: #FECA40; }
#selectable .ui-selected { background: #F39814; color: white; }
#selectable { list-style-type: none; margin: 0; padding: 0; width: 350px;}
#selectable li { margin: 3px; padding: 1px; float: left; width: 100px; height: 80px; font-size: 4em; text-align: center; line-height: 80px; cursor: pointer; cursor: hand; }
</style>

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script type="text/javascript">
 $(function() {
     $( "#selectable" ).selectable({
     stop: function() {
         var result = $( "#select-result" ).empty();
         $( ".ui-selected", this ).each(function() {
             var index = $( "#selectable li" ).index( this );
             result.append( " #" + ( index + 1 ) );
        });
        }
    });
});
</script>
</head>
<body>

<ol id="selectable">
    <?php
        for($i=1;$i<=9;$i++) {
            echo "<li id=\"field_$i\" class=\"ui-state-default\">$i</li>";
        }
    ?>
</ol>
</body>
</html>

最佳答案

<script>

  $(function() { // Start 'ready' listener

    $( "#selectable" ).selectable({
      //... your stuff
    });

    // Select a random <li> element
    var rand = Math.floor(Math.random() * 9) + 1; // return 1 <= rand <= 9
    $("#field_" + rand).click();

  }); // End 'ready' listener

</script>

这是一个简单的例子来说明它是如何工作的:http://jsfiddle.net/Oliboy50/VLetY/

关于jquery - 如何定义由随机数选择的 jQuery selectables?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24991818/

相关文章:

javascript - jQuery UI 的可排序更新事件未触发

javascript - 替换 JQuery 以进行 contenteditable 元素事件绑定(bind)

javascript - 在特定页面上隐藏 div 或 class 元素并在其他页面上显示

html - 为什么 HTML 颜色名称规范中的 'green' 等同于 #008000?

JavaScript:在 1 秒内逐渐将 CSS 属性的值增加 100

javascript - 如何从移动网页打开 Whatsapp 聊天?

手机 : Elements in overflow scroll element ignore border radius

javascript - 仅在选项已更改为默认值时显示

python - 从 QTreeView 中的索引列表中设置选择

android - 如何制作可自定义的按钮来启动应用程序?