2014-10-12 103 views
0

我目前拥有的是这个代码,它在点击add.png图片时继续追加新的下拉列表。 我需要的是点击remove.png图片逐个删除下拉菜单。移除最接近的下拉列表并验证Jquery中的下拉列表

<tr> 
    <td valign="top">Order</td> 
    <td colspan="3"> 
    <div id="salesPersonList"> 
     <select name="sales[]" id="sales" class="sales" style="margin-bottom:1px;"> 
      <option></option> 
      <?php 
       require_once '../model/sales.php'; 
       @$result2=Sales::getAllSalesPerson(); 
       while($value2=mysql_fetch_assoc($result2)){ 
      ?> 
      <option value="<?php echo $value2['id']; ?>"> 
       <?php echo $value2['name'] ?> 
      </option> 
      <?php } ?> 
     </select> 
    <span class="salesPersonEr" style="display:none;color:red;margin-left:30px;">Select the Sales Person Order</span> 
    <img src="../../common/images/add.png" width="20" height="20" name="add" value="Add" onclick="addSalesPerson()" style="margin-left:323px;margin-top:-23px;"/> 
    <img src="../../common/images/remove.png" width="18" height="18" name="del" id="del" value="del" onclick="removeSalesPerson()"/> 
    </div> 
    </td> 

脚本: -

function addSalesPerson(){ 
    var salesPersonListt=$('#sales').html(); 
    $('#salesPersonList').append("<select name='sales[]' id='sales' class='sales' style='margin-bottom:1px'>"+salesPersonListt+"</select>"); 
} 

function removeSalesPerson(){ 
    //Please Help 
} 

的附加部件是完美的,它的工作原理。我需要的是去除部分。 另一部分,我需要的是当我提交按钮(#submit)单击窗体必须进行验证,例如,不应该成为一个下拉选项是: -

  1. 空(也就是没有选择选项值)。
  2. 特定的下拉选项不应等于另一个下拉选项。 (例如: - 在第一个附加下拉菜单中的3-Mark的SalesPerson id以及第五个附加下拉菜单中也包含3-Mark的SalesPerson ID)

任何帮助非常感谢!

+0

欢迎来到计算器。您正在追加具有相同'id'的元素,这是无效的...您应该从'HTMLString'中删除'id'。另外,请避免使用内联样式:[为什么使用CSS](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/Why_use_CSS)。顺便说一句,如果你去掉'PHP'并发布生成的'HTML',你有更好的机会获得帮助......共享代码不包含表单或提交按钮...你发布的代码越少,你得到的准确度较低的答案... – 2014-10-12 05:34:37

回答

0

您可以检查是否存在使用jQuery对象的length财产多个匹配的元素,并使用.remove()方法删除元素:

function removeSalesPerson(){ 
if($('#salesPersonList select').length > 1) 
    $('#salesPersonList select:last').remove(); 
} 

为了验证,你可以使用event.preventDefault()方法,以防止正常形成提交过程和稍后手动提交表单,如果使用验证成功submit()方法:

$("#submit").click(function(e){ 
e.preventDefault(); // prevent normal form submission 
var failure = false, 
emptySelects = $("#salesPersonList select").filter(function(){ // loops through all selects 
    return !this.value; // returns empty selects 
}); 
if(emptySelects.length) // if there are empty selects, simply return. 
    return; 
else{ 
    // loop through each select and compare it's value with the rest of selects 
    $("#salesPersonList select").each(function(){ 
    var val = this.value, // store the value of current select 
    matches = $("#salesPersonList select").not(this).filter(function(){ // loops through rest of the selects 
      return this.value == val; // return selects whose value match the saved value 
    }); 
    if(matches.length){ // if there is any other select with matching value 
     failure = true; // set a boolean indicating loop is exited with a match 
     return false; // exits the each loop since a match is found, no need of further iteration 
    } 
    }); 
} 
if(!failure) // checks whether the loop exited with or without a match 
    $("form")[0].submit(); //if there are no matches submit the form manually 
}); 

其它参考文献:

  • each() - 遍历一个jquery对象
  • filter() - 通过针对每个元素
  • not()执行功能筛一个jquery对象 - 删除从一个jQuery对象匹配的元素

附注:你正在追加元素id,这是无效的...从动态临时删除id属性lates ...

+0

@Starter我更新了关于第一个查询的答案,希望它能起作用。请阅读我的评论下面的问题,并更新与更多的信息.​​..没有示例代码复制的情况下,我们不能测试代码.. – 2014-10-12 10:59:13

+0

@Starter我怎么可以测试第二部分没有样本'HTML''代码..?大多数用户不可能一直运行'PHP'服务器..编辑问题并使用最新的代码以及生成的HTML代码而不是PHP来更新它, – 2014-10-12 11:08:40

+0

@TJ Thanx dude它的工作原理..你节省了我的时间 顺便说一句,你可以解释你写的代码,以便我试着去掌握它,而不是仅仅复制和粘贴。 – Starter 2014-10-12 11:33:54