Javascript jquery clone with HTML下拉选项二下拉选择相同的值警报应选择不同的值
目前正在使用jqueryclone,用户单击add可以完美地克隆div,但我有下拉列表。如果用户在下拉列表中选择手机,如果用户在其他下拉列表中选择相同的手机,则应找到重复的手机,并且必须清除下拉列表值Javascript jquery clone with HTML下拉选项二下拉选择相同的值警报应选择不同的值,javascript,jquery,html,Javascript,Jquery,Html,目前正在使用jqueryclone,用户单击add可以完美地克隆div,但我有下拉列表。如果用户在下拉列表中选择手机,如果用户在其他下拉列表中选择相同的手机,则应找到重复的手机,并且必须清除下拉列表值 $('.slt_major select option:selected').each(function(i, e) { alert("check"); //Check if values match AND if not default AND not match changed i
$('.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');
}
});
即使没有生成警报,我也无法看到错误在哪里。这是你的电话号码
谢谢。就是这样:
首先,我想更正您的添加部分,因为您将重复的id
s添加到克隆行的内部元素
。因此,只需按如下所示更改代码并检查内联注释
$(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.
});
“如果用户选择同一部手机”,您的意思是选择手机作为选项?或者也在其中添加了相同的电话号码?抱歉@psylogic,因为用户已经选择了手机,在新的一行中,我们不想再选择一部手机,他必须选择其他选项。缺少的信息太多,无法为您的问题提供适当的解决方案。请考虑编辑这个问题。我猜我提供的代码是正确的代码,所以你想在这里更改标题,你能给我建议一下吗?gave@Mahadevan您的克隆将彻底崩溃,因为您没有更新id
s,从而在html
Tel中创建重复的id
告诉我你是如何复制这个@Ravikumarad4-5次的。。。你得到了错误。检查这个来吧!!在RaviKumar的台阶上保持清晰。。我添加了4到5次,并更改了newselect
中的值。。工作很好..嗯。。。好的。手机2。业务3。第四银行。空白5。空白现在换成商务或手机,然后告诉我你有多少次得到提醒。哦,是的。。它也在检查空白值。。无论如何。。谢谢你指出。。正在更新答案@拉维库马尔
//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.
}
});
});