最近在做Form表单提交时,使用html标签文本框,表单一直获取不到值,服务器控件就能获取到,最后找到的原因是input的标签没有添加name属性, 具体原因需要查询下ID和Name属性的区别,代码如下:
$.ajax({
type: "POST",
url: "Ajax.aspx",
data: { opt: 'writelog', rowNum: _rNum, ArrayForm: JSON.stringify(getFormJson($('#fmWriteLog'))) },
async: false,
cache: false,
dataType: 'json',
success: function (date) {
if (date <=0) {
alert('保存失败!');
}
else
{
alert('保存成功!');
}
}
});
<form id="fmWriteLog" runat="server" >
<input id="hdLoginName" type="hidden" runat="server" />
<input id="hdRealName" type="hidden" runat="server" />
<input type="hidden" id="hdtxtNum" name="hdtxtNum" value="1"/>
<div id="divlog" style="background-color: white;">
<table id="writelog" style="width: 100%" border="0">
<tr style="font-size: 30px; width: 100%; text-align: center;">
<td colspan="5"><span>工时填写</span> </td>
</tr>
<tr style="font-size: 30px; width: 100%;">
<td colspan="5" style="text-align: right;">
<input id="btnInsert" type="button" οnclick="addTr2('writelog', -2);" value="添加" />
<input id="btnDelete" type="button" οnclick="delTr2()" value="删除" />
</td>
</tr>
<tr>
<td>
<input name='ckb' type="checkbox" /></td>
<td>
<input id='hdtxtProName1' name="hdtxtProName1" type="hidden" runat="server" />
<input id="txtProName1" name="txtProName1" type="text" onclick ='ShowProject(this)' runat="server" /></td>
<td>
<input id="txtDate1" name="txtDate1" type="text" class="Wdate input" οnfοcus="WdatePicker({isShowClear:true,readOnly:false})" /></td>
<td>
<input id="txtHour1" name="txtHour1" type="text" οnkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')" /></td>
</tr>
<tr style="font-size: 30px; width: 100%; text-align: right">
<td colspan="5">
<input type="button" value=" 提 交 " id="btnSubmit" οnclick="BeforSubmit();" />
</td>
</tr>
</table>
</div>
</form>
后台代码:
public partial class EMDC_Work_Ajax : System.Web.UI.Page
{
ManHourBLL manHourBll = new ManHourBLL();
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string opt = Request["opt"] != null ? Request["opt"].Trim() : "";
if (!string.IsNullOrEmpty(opt))
{
HandleOpt(opt);
}
}
}
/// <summary>
/// 处理ajax操作
/// </summary>
/// <param name="opt"></param>
/// <returns></returns>
public void HandleOpt(string opt)
{
switch (opt)
{
case "writelog"://验证是否允许导入
{
Response.Clear();
try
{
List<ManHour> lst = new List<ManHour>();
lst.Clear();
string strValue = Request.Params["ArrayForm"];
int row = Request.Form["rowNum"]==null?0:MyUnity.ObjToInt32(Request.Form["rowNum"]);
string[] array = strValue.Replace("{","").Replace("}","").Replace("\"","").Replace("\"","").Split(',');
for (int i = 1; i <= row; i++)
{
ManHour model = new ManHour();
model.uLoginName =array[0].Split(':')[1];
model.uRealName = array[1].Split(':')[1];
for (int j = 1; j < array.Length; j++)
{
if(array[j].StartsWith("txtDate" + i))
{
model.writeDate = array[j].Split(':')[1];
}
if (array[j].StartsWith("hdtxtProName" + i))
{
model.pCode = MyUnity.ObjToString(array[j].Split(':')[1]);
}
if (array[j].StartsWith("txtHour" + i))
{
model.uHour = MyUnity.ObjToInt32(array[j].Split(':')[1]);
}
}
model.sysDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
lst.Add(model);
}
Response.Write(InsertLog(lst));
Response.End();
}
catch (Exception ex)
{
throw ex;
}
break;
}
}
}
}