如何使用ajax为输入添加值

时间:2016-06-24 19:49:25

标签: javascript c# jquery

我的表有一个字段ID,它是identity,autoincrement和zerofill,以000001开头。好吧,我使用ajax和c#web服务,我的查询是这样的:

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

这是我的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;
}

这是mi class

public class Opc2
{
   public Opc2() { }

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

   public string Item { get; set; }
}

和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);
    }

我的选择获取此值00NaN。

1 个答案:

答案 0 :(得分:5)

请更改您的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>';
}