asp.net 使用 jquery 和 javascript 来确定与按钮匹配的文本框 ID 的中间部分
asp.net using jquery and javascript to determine middle part of ID of textbox that matches button
我有一个按钮,它是 asp.net 网络表单中的遗留应用程序,因此这些生成的 ID 是我必须使用的:
<input type="button" onclick="setDateTimeOn(this);" name="GridView1:_ctl2:AddButton0" value="On" id="GridView1__ctl2_AddButton0" class="btn-blue">
<input name="GridView1:_ctl2:txtStormTimeOn" type="text" id="GridView1__ctl2_txtStormTimeOn">
我有一个我调用的函数
function setDateTimeOn(x){
//alert(x.id);
console.log(x.id);
//$('#x.id').val(
//NEED TO SET textbox to date and time in this format --> 09-11-2015 10:03
}
我需要使用 jquery 或 javascript 来查找 "ctl2" 因此真正解析以找到“_ctl2_AddButton0”之前和 "GridView1__" 之后的字符
我过去使用了一些代码 jquery 和正则表达式,我会在其中使用“^”和部分名称,但在这种情况下我知道格式总是
GridView1__VALUEIWANT_txtStormTimeOn
然后我知道如何用那个值填充我的文本框(有很多行都是用 asp.net 网络表单预先生成的,我不能在这个应用程序中改变它。
Edit/Update
到目前为止,这更接近了
function setDateTimeOn(x){
console.log(x.id); // shows GridView1__ctl2_AddButton0
var re = /__(.+)_/;
// your control name was used an as example:
//var str = 'GridView1__ctl2_AddButton0';
var str = x.id;
var m;
if ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// m[0] == 'ctl2'
console.log(m[0]); // shows __ctl2_
}
编辑 2:
<table>
<tr>
<td style="width:5px;">
<input type="button" name="GridView1:_ctl2:AddButton0" value="On" id="GridView1__ctl2_AddButton0" class="btn-blue">
</td>
<td style="width:150px;">
<input name="GridView1:_ctl2:txtStormTimeOn" type="text" value="asdfasdf" id="GridView1__ctl2_txtStormTimeOn">
</td>
</tr>
</table>
您可以使用通配符来查找包含特定字符串的 ID。
$('[id*="ctl2"]')
如果页面上还有其他元素在其 ID 中包含搜索字符串,您可能需要缩小目标范围。
您可以使用以下代码执行此操作:
var re = /__(.+)_/;
// your control name was used an as example:
var str = 'GridView1__ctl2_AddButton0';
var m;
if ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// m[0] == 'ctl2'
}
试试这个
function setDateTimeOn(elm) {
var formattedDate = GetCurrentDateTime(); //get formatted date
$(elm) //clicked button
.parent("td") // container td
.next() // next td
.find("input") // find child input
.val(formattedDate); //set date
}
function GetCurrentDateTime() {
var now = new Date(),
hours = now.getHours(),
ampm = hours < 12 ? "AM" : "PM";
hours = (hours % 12 ) || 12;
return (now.getMonth() + 1) + "-"
+ now.getDate() + "-"
+ now.getFullYear() + " "
+ hours + ":"
+ pad(now.getMinutes()) + " "
+ ampm;
}
function pad(n) {
return (n < 10) ? ("0" + n) : n;
}
无需省略onclick="setDateTimeOn(this);"
。
我有一个按钮,它是 asp.net 网络表单中的遗留应用程序,因此这些生成的 ID 是我必须使用的:
<input type="button" onclick="setDateTimeOn(this);" name="GridView1:_ctl2:AddButton0" value="On" id="GridView1__ctl2_AddButton0" class="btn-blue">
<input name="GridView1:_ctl2:txtStormTimeOn" type="text" id="GridView1__ctl2_txtStormTimeOn">
我有一个我调用的函数
function setDateTimeOn(x){
//alert(x.id);
console.log(x.id);
//$('#x.id').val(
//NEED TO SET textbox to date and time in this format --> 09-11-2015 10:03
}
我需要使用 jquery 或 javascript 来查找 "ctl2" 因此真正解析以找到“_ctl2_AddButton0”之前和 "GridView1__" 之后的字符
我过去使用了一些代码 jquery 和正则表达式,我会在其中使用“^”和部分名称,但在这种情况下我知道格式总是
GridView1__VALUEIWANT_txtStormTimeOn
然后我知道如何用那个值填充我的文本框(有很多行都是用 asp.net 网络表单预先生成的,我不能在这个应用程序中改变它。
Edit/Update
到目前为止,这更接近了
function setDateTimeOn(x){
console.log(x.id); // shows GridView1__ctl2_AddButton0
var re = /__(.+)_/;
// your control name was used an as example:
//var str = 'GridView1__ctl2_AddButton0';
var str = x.id;
var m;
if ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// m[0] == 'ctl2'
console.log(m[0]); // shows __ctl2_
}
编辑 2:
<table>
<tr>
<td style="width:5px;">
<input type="button" name="GridView1:_ctl2:AddButton0" value="On" id="GridView1__ctl2_AddButton0" class="btn-blue">
</td>
<td style="width:150px;">
<input name="GridView1:_ctl2:txtStormTimeOn" type="text" value="asdfasdf" id="GridView1__ctl2_txtStormTimeOn">
</td>
</tr>
</table>
您可以使用通配符来查找包含特定字符串的 ID。
$('[id*="ctl2"]')
如果页面上还有其他元素在其 ID 中包含搜索字符串,您可能需要缩小目标范围。
您可以使用以下代码执行此操作:
var re = /__(.+)_/;
// your control name was used an as example:
var str = 'GridView1__ctl2_AddButton0';
var m;
if ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// m[0] == 'ctl2'
}
试试这个
function setDateTimeOn(elm) {
var formattedDate = GetCurrentDateTime(); //get formatted date
$(elm) //clicked button
.parent("td") // container td
.next() // next td
.find("input") // find child input
.val(formattedDate); //set date
}
function GetCurrentDateTime() {
var now = new Date(),
hours = now.getHours(),
ampm = hours < 12 ? "AM" : "PM";
hours = (hours % 12 ) || 12;
return (now.getMonth() + 1) + "-"
+ now.getDate() + "-"
+ now.getFullYear() + " "
+ hours + ":"
+ pad(now.getMinutes()) + " "
+ ampm;
}
function pad(n) {
return (n < 10) ? ("0" + n) : n;
}
无需省略onclick="setDateTimeOn(this);"
。