简体   繁体   English

如何使用ajax为输入添加值

[英]How to add value to input with ajax

my table have a field Id which is identity, autoincrement and zerofill, starts with 000001. Thats ok, Im using ajax and c# web service, my query is this: 我的表有一个字段ID,即身份,autoincrement和zerofill,以000001开头。好吧,我使用ajax和c#web服务,我的查询是这样的:

sql = "Select Id FROM table WHERE (Owner ='" + us + "')";

This is my WebMethod 这是我的WebMethod

public string IdRegis(string us)
{
    string resp = "";
    DataTable dt = conn.ConsultTable("id", us);(This is query at top)

    Opc2 op;
    List<Opc2> lista = new List<Opc2>();
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        op = new Opc2((string)dt.Rows[i]["Id"].ToString());
        lista.Add(op);
        op = null;
    }
    JavaScriptSerializer js = new JavaScriptSerializer();
    resp = js.Serialize(lista);
    return resp;
}

And this is mi class 这是mi课程

public class Opc2
{
   public Opc2() { }

   public Opc2(string item)
   {
       Item = item;
   }

   public string Item { get; set; }
}

And ajax success function: 和ajax成功功能:

 success: function (data) {
        var options = '<option disabled="disabled" selected="selected">Select an option...</option>';
        var datas = JSON.parse(data.d);

        for (var i = 0; i < datas.length; i++) {
            var id = ("00000" + (parseInt(datas[i].Id)));
            var zerofillid = id.substring(id.length - 5);

            options += '<option value ="' + zerofillid + '">';
            options += zerofillid;
            options += '</option>';
        }
        $('#id').html(options);
    }

My select gets this values 00NaN. 我的选择获得此值00NaN。

Please change your javascript loop as given below. 请更改您的javascript循环,如下所示。

for (var i = 0; i < datas.length; i++) 
{
    // changed id to Item in this line
    var id = ("00000" + (parseInt(datas[i].Item)));

    var zerofillid = id.substring(id.length - 5);

    optiones += '<option value ="' + zerofillid + '">';
    options += zerofillid;
    options += '</option>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM