【问题标题】:jQuery selectable() elements update valuesjQuery selectable() 元素更新值
【发布时间】:2010-04-30 16:45:53
【问题描述】:

基本上我要做的是在 selectable() UI 停止运行时更新包含在选定元素中的隐藏输入字段的 value 属性。

如果元素被选中,那么输入的值应该是特定 LI 的 name 属性,而如果元素没有被选中,值应该被更新为空。

HTML 示例:

<ul id="selector">
    <li class="networkicon shr-digg" name="shr-digg">
        <div></div>
        <label>Digg</label>
        <input type="hidden" value="" name="bookmark[]" />
    </li>
    <li class="networkicon shr-reddit" name="shr-reddit">
        <div></div>
        <label>Reddit</label>
        <input type="hidden" value="" name="bookmark[]" />
    </li>
    <li class="networkicon shr-newsvine" name="shr-newsvine">
        <div></div>
        <label>Newsvine</label>
        <input type="hidden" value="" name="bookmark[]" />
    </li>
</ul>

脚本示例:

$(function() {
    $("#selector").selectable({ 
        filter: 'li',
        selected: function(event, ui) {
            $(".ui-selected").each(function() {
                $(this).children('input').val($(this).attr('name'));
            });
        },
        unselected: function(event, ui) {
            $(".ui-selected").each(function() {
                $(this).children('input').val('');
            });
        }
    });
});

【问题讨论】:

    标签: jquery jquery-ui selectable


    【解决方案1】:
    $(this).children('input').attr("value", $(this).attr('name'));
    ...
    $(this).children('input').attr("value", '');
    

    这样解决了吗?

    http://api.jquery.com/val/:

    描述:获取当前值 集合中的第一个元素 匹配的元素。

    【讨论】:

    • 是的,当然最简单的方法可以解决问题!哈哈谢谢。
    【解决方案2】:

    您的.each 语法不正确,您似乎将语法与$.each 混淆了。应该是:

    $(".ui-selected").each(function() {
        $(this).children('input').val($(this).attr('name'));
    });
    

    【讨论】:

    • 是的,对不起...发布示例时忘记删除它。
    猜你喜欢
    • 1970-01-01
    • 2015-08-29
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多