我有一个可编辑的Gridview(包含7个可编辑的字段/文本框) 每一行).on按钮(更新)点击我想找到所有文本框的ID,以便我可以检查文本框是否为空。 因为我这样做是作为chrome GridSubMeter_ctl10_txtMeterIdn但中客户端ID的文本框 但客户端ID是在不同的浏览器中动态制作的(在firefox,opera中不同)。
var v = val.id.split('_')[2];
var merter = document.getElementById('GridSubMeter_' + v +'txtMeterIdn_' ).value.trim();
var Billper = document.getElementById('GridSubMeter_' + v + 'txBillPer_' ).value.trim()
var Endkwh = document.getElementById('GridSubMeter_' + v + 'txEndKwh_' ).value.trim();
var startkwh = document.getElementById('GridSubMeter_' + 'txStartKwh_').value.trim();
var ReadEndDate = document.getElementById('GridSubMeter_' + v+'txReadEndDate_' ).value.trim();
var ReadStartDate = document.getElementById('GridSubMeter_' + v+'txReadStartDate_' ).value.trim();
var CTFACT = document.getElementById('GridSubMeter_' + v+ 'txCTFact_' ).value.trim();
JS功能
function fnCheck(val) {
var success = true;
var text1 = val.value;
var v = val.id.split('_')[2];
var merter = document.getElementById('GridSubMeter_' + v +'txtMeterIdn_' ).value.trim();
var Billper = document.getElementById('GridSubMeter_' + v + 'txBillPer_' ).value.trim()
var Endkwh = document.getElementById('GridSubMeter_' + v + 'txEndKwh_' ).value.trim();
var startkwh = document.getElementById('GridSubMeter_' + 'txStartKwh_').value.trim();
var ReadEndDate = document.getElementById('GridSubMeter_' + v+'txReadEndDate_' ).value.trim();
var ReadStartDate = document.getElementById('GridSubMeter_' + v+'txReadStartDate_' ).value.trim();
var CTFACT = document.getElementById('GridSubMeter_' + v+ 'txCTFact_' ).value.trim();
var btn = document.getElementById('GridSubMeter_' + v+'btn_Update_' );
debugger;
if (merter != '') {
}
else {
alert("Meter Identifier is Required Field");
success = false;
}
if (Billper != '') {
}
else {
alert("Bill Period is Required Field");
success = false;
}
if (Endkwh != '') {
}
else {
alert("EndKwh is Required Field");
success = false;
}
if (startkwh != '') {
}
else {
alert("StartKwh is Required Field");
success = false;
}
if (ReadEndDate != '') {
}
else {
alert("Read EndDate is Required Field");
success = false;
}
if (ReadStartDate != '') {
}
else {
alert("Read StartDate is Required Field");
success = false;
}
if (CTFACT != '') {
}
else {
alert("CT Factor is Required Field");
success = false;
}
ASPX按钮代码
<EditItemTemplate>
<asp:Button ID="btn_Update" CausesValidation="false" runat="server" OnClientClick="return fnCheck(this);" Text="Update" CommandName="Update" />
</EditItemTemplate>
答案 0 :(得分:0)
一个简单的解决方案是添加静态的类名,这样即使id是动态输入的,你总能查询上下文。例如,如果每个文本字段都有类textBox
,则可以执行;
var boxes = document.getElementsByClassName("textBoxes");
然后遍历传回的节点列表;
for(var x = 0; x < boxes.length; x++){
console.log(boxes[0].id);
}
答案 1 :(得分:0)
在命名您的ID时,您不应该使用-
,/
或任何其他不是字母数字的字符。
我建议在命名您的ID时使用驼峰案例并在命名您的类时训练案例。使用javascript的ID,样式(CSS)相关代码的类。
通过遵循此惯例,您的ID在浏览器中的名称不会有所不同。