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);
});
});