【发布时间】:2014-11-27 04:52:51
【问题描述】:
好的,这是我的问题。我有一个具有不同值的选择下拉列表。如果我选择其中一个,模板将被克隆到无序列表中。问题是,我想确保你不能克隆同一个元素两次。
在克隆的列表元素中,有一个内部文本与选择下拉列表中选项的相应标签相同。
唉,我尝试在无序列表中找到与下拉列表中当前选择的选项具有相同文本的标签。如果有这样的标签,就不应该继续克隆。
这是我的代码:
$ ->
$('#add_feature').change -> //#add_feature is the id of my select option dropdown
features = $('ul#features') //features is the unordered list, where i append my
//cloned list elements
...
selected = $(this).find('option:selected') //here i find the currently selected
//option
//this is where i want to have an if clause or something that compares the
//inner text of all the labels in the UL to the text of the currently
//selected option
//this is the cloning procedure which should only be called if there is
//no label found above
feature = $('#template_feature li').clone()
features.append(feature)
$(feature).find('#home_features_attributes_new_type').val(selected.data('type'))
$(feature).find('#home_features_attributes_new_name').val($(this).val())
$(feature).find('label[for="home_features_attributes"]').prepend(selected.text())
【问题讨论】:
标签: javascript jquery html coffeescript