2014-03-03 22 views
0

已经添加了一些新的MVC 3项目的jQuery,但jQuery代码似乎并没有工作。jquery代码似乎没有工作 - MVC 3

HTML和脚本如下:

<div class="LineItems"> 

     <div class="LineItemHeading"> 
      <span>ItemCode</span> 
      <span>Description</span> 
      <span>Qty</span> 
      <span>UnitPrice</span> 
      <span>Acc.Code</span> 
      <div class="clear">&nbsp;</div> 
     </div> 

     <div class="LineItem"> 
      <input type="text" class="ItemCode" /> 
      <input type="text" class="ItemDescription" /> 
      <input type="text" class="ItemQty" /> 
      <input type="text" class="ItemPrice" /> 
      <input type="text" class="ItemAccCode" /> 
     </div> 

     <button id="AddLineItem">Add Line Item</button> 
     <button id="RemoveLineItem">Remove Line Item</button> 
    </div> 



<script type="text/javascript"> 

    $("#AddLineItem").click(function() { 
     alert("function called.."); // the alert shows. 
     $(".LineItem:last-child").clone().appendTo($(".LineItem")); 
     $(".LineItem:last-child input").each(function() { 
     $(this).val(""); 
     }); 
    }); 

</script> 

目标是复制的最后一行,并追加到尾部。 (创建一个与最后一行相同的新行。)

jquery似乎被添加到主布局和铬开发人员工具似乎没有给任何jQuery错误。

任何帮助表示赞赏。

+0

当您将val()设置为空时,从val()中除去引号。 –

+0

的意图。当订单项div被复制时,其值被设置为空白。是不是这样做的正确方法 –

+0

这里所期望的结果是什么?该输入被添加,然后设置为空白? –

回答

0

您没有正确针对您的选择器的输入。尝试这些更改 - http://jsfiddle.net/aT8Bk/

$("#AddLineItem").click(function() { 
    alert("function called.."); // the alert shows. 
    $(".LineItem input:last-child").clone().appendTo($(".LineItem")); 
    $(".LineItem input:last-child").each(function() { 
     $(this).val(""); 
    }); 
}); 
+0

,但这样做不仅会复制最后一个输入字段。我希望克隆整个Last div并追加它。 –

+0

编辑:看到这个更新http://jsfiddle.net/aT8Bk/2/你可能想要修改你的容器divs来更好地安排这个。 –

+0

感谢您的时间和答案。这个问题似乎是一个错字。 ()函数调用(“function called ..”); $(“。LineItem:last-child”)。clone()。appendTo($(“#”)) .LineItems“)); $(”。LineItem:last-child input“)。each(function(){(this).val(”“); }); }); –