[英]How to disable specific input fields on focus?
I have GridView
that renders a table.我有呈现表格的
GridView
。 This table has text fields and input fields.此表具有文本字段和输入字段。
I wrote the following jQuery
to disable text fields on focus.我编写了以下
jQuery
来禁用焦点上的文本字段。 It works fine:它工作正常:
$(function(){
$('input[type="text"]').live("focus", function() {
var rowId = $(this).attr('id');
var inputField = $(this).attr('name');
alert(inputField.indexOf('txtSearchValue'));
$('tr').each(function(){
var inputId = $(this).find('input[type="text"]').attr('id');
var submitId = $(this).closest("tr").find('input[type="submit"]').attr('id');
if(inputId != null && typeof inputId !== "undefined"){
if(rowId !== inputId){
$(this).find('input[type="text"]').attr("disabled", "disabled");
$(this).closest("tr").find('input[type="submit"]').attr("disabled","disabled");
}
}
});
});
});
I also have another text field that is not a part of a generated table but it also gets disabled every time I click on textfields belonging to the table.我还有另一个文本字段,它不是生成表的一部分,但每次单击属于该表的文本字段时它也会被禁用。
This is a text field:这是一个文本字段:
<input id="ctl00_ContentMain_txtSearchValue" type="text" value="2222" name="ctl00$ContentMain$txtSearchValue"></input>
I'm trying to use:我正在尝试使用:
var inputField = $(this).attr('name'); inputField.indexOf('txtSearchValue');
to access that extra field, but I'm not sure how to combine it with my jQuery
to make sure it is always enabled.访问该额外字段,但我不确定如何将它与我的
jQuery
结合以确保它始终处于启用状态。
Any ideas?有任何想法吗?
How can I modify it, so, the extra field我该如何修改它,所以,额外的字段
This is my html table:这是我的 html 表:
<TABLE style="POSITION: absolute; BORDER-BOTTOM-COLOR: red; BORDER-TOP-COLOR: red; BORDER-COLLAPSE: collapse; BORDER-RIGHT-COLOR: red; BORDER-LEFT-COLOR: red; TOP: 150px" id=ctl00_ContentMain_grvIsoSearchResults class=DataWebControlStyle border=1 rules=all cellSpacing=0>
<TBODY>
<TR class=HeaderStyle>
<TH scope=col>ISONUM</TH>
<TH scope=col>OFFICE NAME</TH>
<TH scope=col>REGION</TH>
<TH scope=col>DIVISION</TH>
<TH scope=col>EMAIL ADDRESS</TH></TR>
<TR class=RowStyle>
<TD><SPAN style="TEXT-ALIGN: center; WIDTH: 70px; DISPLAY: inline-block" id=ctl00_ContentMain_grvIsoSearchResults_ctl02_txtgvIsoNum>222222222 </SPAN>
</TD>
<TD><SPAN style="TEXT-ALIGN: center; WIDTH: 200px; DISPLAY: inline-block" id=ctl00_ContentMain_grvIsoSearchResults_ctl02_txtgvIsoOfficeName>Test, Eugene Test </SPAN>
</TD>
<TD><SPAN style="TEXT-ALIGN: center; WIDTH: 50px; DISPLAY: inline-block" id=ctl00_ContentMain_grvIsoSearchResults_ctl02_txtgvRegion>99</SPAN>
</TD>
<TD><SPAN style="TEXT-ALIGN: center; WIDTH: 50px; DISPLAY: inline-block" id=ctl00_ContentMain_grvIsoSearchResults_ctl02_txtgvDivision>11111</SPAN>
</TD>
<TD><INPUT id=ctl00_ContentMain_grvIsoSearchResults_ctl02_txtgvEmailAddress class=textInput name=ctl00$ContentMain$grvIsoSearchResults$ctl02$txtgvEmailAddress value=test@test.com>
<INPUT id=ctl00_ContentMain_grvIsoSearchResults_ctl02_btnEmailUpdate onclick="return ValidateEmail(this);" name=ctl00$ContentMain$grvIsoSearchResults$ctl02$btnEmailUpdate value=Update type=submit>
</TD>
</TR>
<TR class=AlternatingRowStyle>
<TD><SPAN style="TEXT-ALIGN: center; WIDTH: 70px; DISPLAY: inline-block" id=ctl00_ContentMain_grvIsoSearchResults_ctl03_txtgvIsoNum>CB2222001 </SPAN>
</TD>
<TD><SPAN style="TEXT-ALIGN: center; WIDTH: 200px; DISPLAY: inline-block" id=ctl00_ContentMain_grvIsoSearchResults_ctl03_txtgvIsoOfficeName>DENNIS PETROVIC </SPAN>
</TD>
<TD><SPAN style="TEXT-ALIGN: center; WIDTH: 50px; DISPLAY: inline-block" id=ctl00_ContentMain_grvIsoSearchResults_ctl03_txtgvRegion></SPAN>
</TD>
<TD><SPAN style="TEXT-ALIGN: center; WIDTH: 50px; DISPLAY: inline-block" id=ctl00_ContentMain_grvIsoSearchResults_ctl03_txtgvDivision>99801</SPAN>
</TD>
<TD>
<INPUT id=ctl00_ContentMain_grvIsoSearchResults_ctl03_txtgvEmailAddress class=textInput name=ctl00$ContentMain$grvIsoSearchResults$ctl03$txtgvEmailAddress value=dennis@dlgent.com>
<INPUT id=ctl00_ContentMain_grvIsoSearchResults_ctl03_btnEmailUpdate onclick="return ValidateEmail(this);" name=ctl00$ContentMain$grvIsoSearchResults$ctl03$btnEmailUpdate value=Update type=submit>
</TD>
</TR>
<TR class=RowStyle>
<TD><SPAN style="TEXT-ALIGN: center; WIDTH: 70px; DISPLAY: inline-block" id=ctl00_ContentMain_grvIsoSearchResults_ctl04_txtgvIsoNum>FT2222001 </SPAN>
</TD>
<TD><SPAN style="TEXT-ALIGN: center; WIDTH: 200px; DISPLAY: inline-block" id=ctl00_ContentMain_grvIsoSearchResults_ctl04_txtgvIsoOfficeName>DENNIS PETROVIC </SPAN>
</TD>
<TD><SPAN style="TEXT-ALIGN: center; WIDTH: 50px; DISPLAY: inline-block" id=ctl00_ContentMain_grvIsoSearchResults_ctl04_txtgvRegion></SPAN>
</TD>
<TD><SPAN style="TEXT-ALIGN: center; WIDTH: 50px; DISPLAY: inline-block" id=ctl00_ContentMain_grvIsoSearchResults_ctl04_txtgvDivision>99801</SPAN>
</TD>
<TD><INPUT id=ctl00_ContentMain_grvIsoSearchResults_ctl04_txtgvEmailAddress class=textInput name=ctl00$ContentMain$grvIsoSearchResults$ctl04$txtgvEmailAddress value=dennis@dlgent.com>
<INPUT id=ctl00_ContentMain_grvIsoSearchResults_ctl04_btnEmailUpdate onclick="return ValidateEmail(this);" name=ctl00$ContentMain$grvIsoSearchResults$ctl04$btnEmailUpdate value=Update type=submit>
</TD>
</TR>
</TBODY>
</TABLE>
You attach a function to all 'input[type="text"]'
.您将一个函数附加到所有
'input[type="text"]'
。 This means that all the input fields on your page will have this function.这意味着您页面上的所有输入字段都将具有此功能。 You might want to check out this question.
你可能想看看这个问题。
Your query should looks something like this: table > input[type="text"]
.您的查询应如下所示:
table > input[type="text"]
。 For more details .欲了解更多详情。
UPDATE更新
Looking at your code I am not sure your system does what you really want.查看您的代码,我不确定您的系统是否满足您的需求。 Anyway, here is a filldle that gives you the fields with
inputText
classes from your table.无论如何,这里有一个filldle ,它为您提供了表格中带有
inputText
类的字段。 Then you can attach to them a function (Just pop up the console -- in Chrome F12 and you'll see the selected elements of the page).然后你可以给它们附加一个函数(只需弹出控制台——在 Chrome F12 中,你就会看到页面的选定元素)。
As I said you need to restrict your selector level by level:正如我所说,您需要逐级限制选择器:
$('td > .textInput').each(function(){
console.log(this);
})
So you select all the .textInput
classes from all td
tags.因此,您从所有
td
标记中选择所有.textInput
类。 This will work without just uwing $('.textInput')
in case you don't have any other td
tags.如果您没有任何其他
td
标签,这将在不使用 uwing $('.textInput')
的情况下工作。 If you have multiple tables use the id of the table you want to use in the selector then descend from there to the required level.如果您有多个表,请使用要在选择器中使用的表的 id,然后从那里下降到所需的级别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.