如何避免动态生成的行中的下拉字段中的重复值

时间:2012-03-27 16:14:20

标签: jquery jquery-ui symfony twig

我在我的twig中使用宏来实现动态添加行功能,宏部分如下:

{% macro macroLeave(item, hrisFlag) %}
            <tr>
                <td width="28%"  align="left" height="30" bgcolor="#f6f6f6">
                    {{ form_widget(item.leaveType) }}
                    {{ form_errors(item.leaveType) }} 
                </td>
            </tr>
{% endmacro %} 

必要的javascript片段如下:

var addLeaveRow = function() 
        {
            var index = $('#tblLeave tbody tr').length;
            var row = $('script[name="scriptLeave"]').text().replace(/\$\$name\$\$/g, index);
            $('#tblLeave tbody').append(row);
        }

有人可以建议一种实现此功能的方法吗?

1 个答案:

答案 0 :(得分:0)

这是一种方法:

// keep track of the id's 'name of leave' that must be unique.
var allLeaves = {};

// ... before you add it, check if it exists.
if (typeof(allLeaves['name of leave']) === "undefined") {
    allleaves['name of leave'] = true;
    // ... now do your stuff.
} else {
    // the leave already exists, so perhaps focus it for the user?
}