admin管理员组文章数量:1026989
I have a textbox with these rules: 1) I populate the textbox.text from a dictionary in session 2) If the user enters a new value, setTextBoxData will save it in the dictionary 3) On entry (on focus) the field text is blanked. 4) On blur, if the field is still empty, I want to set it to the original value.
<asp:TextBox ID="txtNumberEmployees" runat="server" Width="50px" onfocus="this.value='';"
onchange= "javaScript:$(function()setTextBoxData('NUMBEREMPLOYEES','txtNumberEmployees');});"
onblur="javaScript:restore ('txtNumberEmployees', 'NUMBEREMPLOYEES');"/>
The "restore" function referenced above is:
function restore(control, input) {
var data = getInputData(input);
$('#' + control).val(data);
}
getInputData returns the data value correctly. The problem is with the last line.
I have tried many ways to set this, but none seem to work. It should be a simple problem, but I can't get it to work yet.
I have a textbox with these rules: 1) I populate the textbox.text from a dictionary in session 2) If the user enters a new value, setTextBoxData will save it in the dictionary 3) On entry (on focus) the field text is blanked. 4) On blur, if the field is still empty, I want to set it to the original value.
<asp:TextBox ID="txtNumberEmployees" runat="server" Width="50px" onfocus="this.value='';"
onchange= "javaScript:$(function()setTextBoxData('NUMBEREMPLOYEES','txtNumberEmployees');});"
onblur="javaScript:restore ('txtNumberEmployees', 'NUMBEREMPLOYEES');"/>
The "restore" function referenced above is:
function restore(control, input) {
var data = getInputData(input);
$('#' + control).val(data);
}
getInputData returns the data value correctly. The problem is with the last line.
I have tried many ways to set this, but none seem to work. It should be a simple problem, but I can't get it to work yet.
Share Improve this question edited Oct 26, 2012 at 21:57 Adriano Carneiro 58.7k12 gold badges94 silver badges123 bronze badges asked Oct 26, 2012 at 19:18 Bob JonesBob Jones 2,0485 gold badges34 silver badges60 bronze badges 1-
looks like a syntax error:
javaScript:$(function()setTextBoxData('NUMBEREMPLOYEES','txtNumberEmployees');});
shouldn't it just bejavaScript:setTextBoxData('NUMBEREMPLOYEES','txtNumberEmployees');
– Brian Glaz Commented Oct 26, 2012 at 19:22
2 Answers
Reset to default 1The problem is ASP.NET will generate an ID that will not be txtNumberEmployees
. ASP.NET will generate an ID for your input that will end with txtNumEmployees
.
Change this line:
$('#' + control).val(data);
to this:
$('[id$=' + control + ']').val(data);
It will work because this is the Attribute Ends with Selector.
1: Make sure you have no javascript errors. I see there's a missing '{' in the onchange.
2: You can simply pass 'this' as the textbox reference and update it like below:
<asp:TextBox ID="txtNumberEmployees" runat="server" Width="50px" onfocus="this.value='';"
onchange= "javaScript:$(function(){setTextBoxData('NUMBEREMPLOYEES',this);});"
onblur="javaScript:restore (this, 'NUMBEREMPLOYEES');"/>
then simply set the value like:
$(control).val(data);
3: There are other ways as well to grab an asp element like shown here. Find ASP.NET ClientID in jquery
I have a textbox with these rules: 1) I populate the textbox.text from a dictionary in session 2) If the user enters a new value, setTextBoxData will save it in the dictionary 3) On entry (on focus) the field text is blanked. 4) On blur, if the field is still empty, I want to set it to the original value.
<asp:TextBox ID="txtNumberEmployees" runat="server" Width="50px" onfocus="this.value='';"
onchange= "javaScript:$(function()setTextBoxData('NUMBEREMPLOYEES','txtNumberEmployees');});"
onblur="javaScript:restore ('txtNumberEmployees', 'NUMBEREMPLOYEES');"/>
The "restore" function referenced above is:
function restore(control, input) {
var data = getInputData(input);
$('#' + control).val(data);
}
getInputData returns the data value correctly. The problem is with the last line.
I have tried many ways to set this, but none seem to work. It should be a simple problem, but I can't get it to work yet.
I have a textbox with these rules: 1) I populate the textbox.text from a dictionary in session 2) If the user enters a new value, setTextBoxData will save it in the dictionary 3) On entry (on focus) the field text is blanked. 4) On blur, if the field is still empty, I want to set it to the original value.
<asp:TextBox ID="txtNumberEmployees" runat="server" Width="50px" onfocus="this.value='';"
onchange= "javaScript:$(function()setTextBoxData('NUMBEREMPLOYEES','txtNumberEmployees');});"
onblur="javaScript:restore ('txtNumberEmployees', 'NUMBEREMPLOYEES');"/>
The "restore" function referenced above is:
function restore(control, input) {
var data = getInputData(input);
$('#' + control).val(data);
}
getInputData returns the data value correctly. The problem is with the last line.
I have tried many ways to set this, but none seem to work. It should be a simple problem, but I can't get it to work yet.
Share Improve this question edited Oct 26, 2012 at 21:57 Adriano Carneiro 58.7k12 gold badges94 silver badges123 bronze badges asked Oct 26, 2012 at 19:18 Bob JonesBob Jones 2,0485 gold badges34 silver badges60 bronze badges 1-
looks like a syntax error:
javaScript:$(function()setTextBoxData('NUMBEREMPLOYEES','txtNumberEmployees');});
shouldn't it just bejavaScript:setTextBoxData('NUMBEREMPLOYEES','txtNumberEmployees');
– Brian Glaz Commented Oct 26, 2012 at 19:22
2 Answers
Reset to default 1The problem is ASP.NET will generate an ID that will not be txtNumberEmployees
. ASP.NET will generate an ID for your input that will end with txtNumEmployees
.
Change this line:
$('#' + control).val(data);
to this:
$('[id$=' + control + ']').val(data);
It will work because this is the Attribute Ends with Selector.
1: Make sure you have no javascript errors. I see there's a missing '{' in the onchange.
2: You can simply pass 'this' as the textbox reference and update it like below:
<asp:TextBox ID="txtNumberEmployees" runat="server" Width="50px" onfocus="this.value='';"
onchange= "javaScript:$(function(){setTextBoxData('NUMBEREMPLOYEES',this);});"
onblur="javaScript:restore (this, 'NUMBEREMPLOYEES');"/>
then simply set the value like:
$(control).val(data);
3: There are other ways as well to grab an asp element like shown here. Find ASP.NET ClientID in jquery
本文标签: javascriptUsing JQuery onblur to set textbox valueStack Overflow
版权声明:本文标题:javascript - Using JQuery onblur to set textbox value - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745665786a2162169.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论