Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据用户从此jQuery UI可选下拉框中选择的内容创建链接_Php_Javascript_Jquery Ui - Fatal编程技术网

Php 根据用户从此jQuery UI可选下拉框中选择的内容创建链接

Php 根据用户从此jQuery UI可选下拉框中选择的内容创建链接,php,javascript,jquery-ui,Php,Javascript,Jquery Ui,我用这个: 这是我的代码: <ol id="selectable"> <?php foreach ($collection as $key=> $word): ?> <li class="ui-widget-content"> <?php echo $word ?> </li> <?php endforeach ?> </ol> 如何根据用户选择的内容创建超链接?每次使用的

我用这个:

这是我的代码:

<ol id="selectable">
    <?php foreach ($collection as $key=> $word): ?>
    <li class="ui-widget-content"> <?php echo $word ?> </li>
    <?php endforeach ?>
</ol>

  • 如何根据用户选择的内容创建超链接?每次使用的单词都不同。

    使用事件:

    ui.selected
    是所选项目的DOM元素

    如果向
    li
    添加
    数据
    属性,并提供
    href
    的更多详细信息,则会更容易:

    <ol id="selectable">
        <?php foreach ($collection as $key=> $word): ?>
        <li class="ui-widget-content" data-href="<?php echo $href ?>"> <?php echo $word ?> </li>
        <?php endforeach ?>
    </ol>
    

    要使用所选项目创建链接(锚定),可以执行以下操作:

    $(function() {
        $("#selectable").selectable({
            selected: function(event, ui) {
                $item = $(ui.selected);
                $href = $item.data('href');
                $text = $item.text();
    
                $('<a />').attr({
                    href: $href
                }).text($text).append('<br />').appendTo('#links');
            }
        });
    });​
    

    非常感谢。有可能只有一个提交链接吗?然后当它被按下时,它将根据选择生成一个URL?@flux当然-只需将所选值存储在某个位置(例如隐藏输入),然后在按下提交按钮时生成URL,使用该存储无法使其正常工作。我不知道任何jquery。我应该以你的第三个例子为基础吗?如何存储变量而不被覆盖?谢谢
    $(function() {
        $("#selectable").selectable({
            selected: function(event, ui) {
                alert($(ui.selected).data('href'));
            }
        });
    });​
    
    $(function() {
        $("#selectable").selectable({
            selected: function(event, ui) {
                $item = $(ui.selected);
                $href = $item.data('href');
                $text = $item.text();
    
                $('<a />').attr({
                    href: $href
                }).text($text).append('<br />').appendTo('#links');
            }
        });
    });​
    
    // create temp variable to store the selected element
    var selected;
    
    $(function() {
        $("#selectable").selectable({
            selected: function(event, ui) {
               selected = ui.selected;
            }
    
        });
    
        $('#getSel').click(function() {
            // do what you want with the Element here
            alert(selected.innerHTML);
        });
    });