jQuery selectable()元素更新值

时间:2010-04-30 16:45:53

标签: jquery jquery-ui selectable

基本上我要做的是当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('');
            });
        }
    });
});

2 个答案:

答案 0 :(得分:1)

$(this).children('input').attr("value", $(this).attr('name'));
...
$(this).children('input').attr("value", '');

这解决了吗?

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

  

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

答案 1 :(得分:1)

您的.each语法不正确,您似乎将语法与$.each混合在一起。它应该是:

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