目前正在使用 jquery 克隆,用户单击添加它将完美克隆 div,但我有下拉菜单。如果用户在下拉列表中选择手机,并且在其他下拉列表中选择相同的手机,则应该找到重复的手机,并且下拉值必须清除。
$('.slt_major select option:selected').each(function(i, e) {
alert("check");
//Check if values match AND if not default AND not match changed item to self
if ($(e).val() == cI.val() && $(e).val() != 0 && $(e).parent().index() != cI.index()) {
alert('Duplicate found!');
cI.val('0');
}
});
即使没有生成警报,我也无法看到错误在哪里。这是fiddle link .
谢谢。
最佳答案
所以这里是: DEMO
首先,我想更正您的添加部分,因为您将重复的id
添加到克隆行<的内部
。因此,只需按如下所示更改代码并检查内联注释即可。元素
中
$(document).on("click", ".btn_more", function () {
var $clone = $('.cloned-row:eq(0)').clone();
$clone.find('[id]').each(function(){
this.id=this.id +(count) //change the id of each element by adding count to it
});
$clone.find('.btn_more').after("<input type='button' class='btn_less1 phn_del' value='Del' id='buttonless"+count+"'/>")
$clone.attr('id', "added"+(count)); //just append count here
$clone.find('.preferred').attr('checked', false);
$clone.find('.sslt_Field').val(0);
$clone.find('.txt_CC').val('');
$clone.find('.txt_Pno').val('');
$(this).parents('.em_pho').after($clone);
count++; //increment count at the end.
});
现在要检查重复的选项
,您可以按如下方式执行。另请检查内嵌注释:
//attach event handler to document since you need event delegation on dynamically created elements
//attach change event to class 'sslt_Field'
$(document).on('change','select.sslt_Field',function(event) {
var cI = $(this); //store a reference
var others=$('select.sslt_Field').not(cI);
//store reference to other select elements except the selected one
$.each(others,function(){
//iterate through remaining selects
if($(cI).val()==$(this).val() && $(cI).val()!="")//check if value has been
//already selected on other select
{
$(cI).val('');//empty the value
alert('already selected');//display alert.
}
});
});
关于javascript - 带有 HTML 下拉选项的 jquery 克隆 两个下拉菜单选择相同的值,警报应选择不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33007979/