如何在jquery中为动态创建的文本框赋值?

时间:2014-03-29 13:01:49

标签: jquery

我使用jquery为产品名称和产品ID创建了动态文本框, 我为每个文本框创建了唯一的ID,我想为文本框分配值。 我的代码如下

<script type="text/javascript">

$(document).ready(function () {
    var rIndex = $(".ProductName").length;
    var ndate = new Date();
    var time = ndate.getMilliseconds();

    var i = rIndex + time;
    var a1, a2;


    $("#lnkAddProduct").on("click", function () {


     var d = '<div class="addedProduct "><div class="control-group " style="margin-bottom:10px;"><input type="text" class="ProductName span2 pull-left" id="Product_Name_' + i + '" name="Product_Name" value="" placeholder="start typing to load products")"/><input type="text" class="ProductId" name="Product_Id" id="Product_Id_' + i + '" value="" /><a href="#" class="clsremove font-midium" style="margin-left:5px;margin-top:5px;" id="remove_' + i + '">remove</a></div></div>';

        $('#ProductList').append(d);
        alert("newID:"+i);

        //$(".clsremove").unbind("click").click(function () {
        //    $(this).closest(".addedProduct").remove();
        //});


        $('body').delegate(".ProductName", "focusin", function () {

            $(this).autocomplete({
                source: '@Url.Action("NewvendorList","Product")',

            minLength: 1,

            select: function (evt, ui) {
                 a1 = ui.item.label;
                 a2 = ui.item.id;

                alert("label" + ui.item.label);
                alert("id" + ui.item.id);

                 $('#Product_Name_'+i).val(a1);
                alert("i===" + i);

                $('#Product_Id_'+i).val(a2)

            },
        });
      });
        i = i + 1;          
    });
}); 

$(&#39; #Product_Name _&#39; + i).val(a1)此行错误发生为未定义。 如何为文本框赋值给值。

1 个答案:

答案 0 :(得分:0)

$(document).ready(function () {
  var rIndex = $(".ProductName").length;
  var ndate = new Date();
  var time = ndate.getMilliseconds();

  var i = rIndex + time;
  var a1, a2;


  $("#lnkAddProduct").on("click", function () {


  var d = '<div class="addedProduct "><div class="control-group " style="margin-bottom:10px;"><input type="text" class="ProductName span2 pull-left" id="Product_Name_' + i + '" name="Product_Name" value="" placeholder="start typing to load products")"/><input type="text" class="ProductId" name="Product_Id" id="Product_Id_' + i + '" value="" /><a href="#" class="clsremove font-midium" style="margin-left:5px;margin-top:5px;" id="remove_' + i + '">remove</a></div></div>';

    $('#ProductList').append(d);
    alert("newID:"+i);

    //$(".clsremove").unbind("click").click(function () {
    //    $(this).closest(".addedProduct").remove();
    //});


    $(".ProductName").on("focusin", function () {

        $(this).autocomplete({
            source: '@Url.Action("NewvendorList","Product")',

        minLength: 1,

        select: function (evt, ui) {
             a1 = ui.item.label;
             a2 = ui.item.id;

            alert("label" + ui.item.label);
            alert("id" + ui.item.id);

             $('#Product_Name_'+i).val(a1);
            alert("i===" + i);

            $('#Product_Id_'+i).val(a2)

        },
    });
  });
    i = i + 1;          
  });
 }); 
 replace your code with above and test it