简体   繁体   English

如何获取可选jquery元素的innerHTML?

[英]How to get the innerHTML of selectable jquery element?

I have a php generated list whose list items are selectable using jquery selectable widget. 我有一个php生成的列表,其列表项可以使用jquery selectable widget选择。 The list for all intents and purposes is: 所有意图和目的的列表是:

<ul id="#select-image">
    <li class="ui-widget-content">Item 1</li>
    <li class="ui-widget-content">Item 2</li>
    <li class="ui-widget-content">Item 3</li>
    <li class="ui-widget-content">Item 4</li>
    <li class="ui-widget-content">Item 5</li>
    <li class="ui-widget-content">Item 6</li>
    <li class="ui-widget-content">Item 7</li>
</ul>

And the jQuery selectable is declared as: 并且jQuery selectable声明为:

<script>
    $(function() {
        $("#select-image").selectable({
            selected: function( event, ui ) { 
                var $variable = $('.ui-selected').innerHTML; 
                console.log($variable);
            }
        });
    });
</script>

An event takes place after a list item has been selected, in the example it outputs to the browser console. 在选择列表项之后发生事件,在示例中它将输出到浏览器控制台。 The output however is "undefined." 然而输出是“未定义的”。 The selector $('.ui-selected'). 选择器$('.ui-selected'). is correct as it shows as an object in the browser's console. 是正确的,因为它在浏览器的控制台中显示为一个对象。 Where am I going wrong? 我哪里错了?

Try 尝试

.text() or .html() instead of .innerHTML .text().html()而不是.innerHTML

Use .val() instead of .innerHTML for getting value of selected option 使用.val()代替.innerHTML来获取所选选项的值

Use .text() for getting text of selected option 使用.text()获取所选选项的文本

Thanks for correcting :) 谢谢你纠正:)

$(function() {
        $("#select-image").selectable({
            selected: function( event, ui ) { 
                var $variable = $('.ui-selected').html(); 
                console.log($variable);
            }
        });
    });

or 要么

$(function() {
        $("#select-image").selectable({
            selected: function( event, ui ) { 
                var $variable = $('.ui-selected').text(); 
                console.log($variable);
            }
        });
    });

or 要么

$(function() {
        $("#select-image").selectable({
            selected: function( event, ui ) { 
                var $variable = $('.ui-selected').val(); 
                console.log($variable);
            }
        });
    });

The parameter ui has a property called selected which is a reference to the selected dom element, you can call innerHTML on that element. 参数ui有一个名为selected的属性,它是对所选dom元素的引用,你可以在该元素上调用innerHTML

Your code $('.ui-selected').innerHTML tries to return the innerHTML property of a jQuery wrapper element for a dom element with class ui-selected 您的代码$('.ui-selected').innerHTML尝试为类ui-selected的dom元素返回jQuery包装元素的innerHTML属性

$(function () {
    $("#select-image").selectable({
        selected: function (event, ui) {
            var $variable = ui.selected.innerHTML; // or $(ui.selected).html()
            console.log($variable);
        }
    });
});

Demo: Fiddle 演示: 小提琴

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

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