Iam new @ jquery - Thats working for almost all versions of IE except IE9 it says "Error: 'f' is undefined" i didnt know why ?? and if its not recommended to giving function name like that way then what should i do ?
$(document).ready(function f(txtName)
{
$("#ctl00_Content_chkBox" + txtName).click(function ()
{
var thisCheck = $(this);
if (thisCheck.is(':checked'))
{
var myDate = new Date();
var displayDate =
(myDate.getMonth() + 1) + '/' + (myDate.getDate()) + '/' + myDate.getFullYear();
$(this).next(".textbox1").val(displayDate);
}
});
});
<input type="checkbox" ID="chkBoxName" runat="server" onclick="f('Name');" />
<asp:TextBox CssClass="textbox1" PreValue="" runat="Server" ID="txt_comp_name_date_v" Required="false" Enab开发者_如何学编程led="False" />
You're passing a named function expression to $(document).ready
. Previous versions of Internet Explorer had a bug which exposed the function into the surrounding scope. It appears Internet Explorer 9 has fixed this bug that you were relying on. Use a function declaration rather than a named function expression by not passing it to $(document).ready
:
function f(txtName) {
$("#ctl00_Content_chkBox" + txtName).click(function () {
var thisCheck = $(this);
if (thisCheck.is(':checked')) {
var myDate = new Date();
var displayDate = (myDate.getMonth() + 1) + '/' + (myDate.getDate()) + '/' + myDate.getFullYear();
$(this).next(".textbox1").val(displayDate);
}
});
}
You only use $(document).ready()
with code that you want to run at initialization time. You do not use it for named functions that you want to run later. You look like you were trying to do some of both with the same code.
I would suggest changing your code like this. I've removed the name on the function and removed the onclick=f()
from your HTML that referred to it as you don't need either. Now, there is a click handler that is run in the $(document).ready()
that hooks up the click handler function for you (no need for onclick="f()"
any more). The code for the click handler then carries out the rest of your work.
$(document).ready(function() {
$("#chkBoxName").click(function () {
var thisCheck = $(this);
if (thisCheck.is(':checked')) {
var myDate = new Date();
var displayDate = (myDate.getMonth() + 1) + '/' + (myDate.getDate()) + '/' + myDate.getFullYear();
thisCheck.next(".textbox1").val(displayDate);
}
});
});
<input type="checkbox" ID="chkBoxName" runat="server" />
<asp:TextBox CssClass="textbox1" PreValue="" runat="Server" ID="txt_comp_name_date_v" Required="false" Enabled="False" />
You can see it work here: http://jsfiddle.net/jfriend00/9y7sC/.
You should be very careful with this part of the code thisCheck.next(".textbox1")
because that is very picky about finding your text field. If you put any intervening HTML tag in between the two, it won't work - it has to be the very next HTML tag and it has to have the right class.
If you have a lot of checkboxes next to text fields and you want them all to get this function, then just give them all the same class name and refer to that class in the jQuery code like this:
$(document).ready(function() {
$(".dateCheckbox").click(function () {
var thisCheck = $(this);
if (thisCheck.is(':checked')) {
var myDate = new Date();
var displayDate = (myDate.getMonth() + 1) + '/' + (myDate.getDate()) + '/' + myDate.getFullYear();
thisCheck.next(".textbox1").val(displayDate);
}
});
});
<input type="checkbox" class="dateCheckbox" runat="server" />
<asp:TextBox CssClass="textbox1" PreValue="" runat="Server" ID="txt_comp_name_date_v" Required="false" Enabled="False" />
Here's an example of multiple checkboxes working off the one piece of code: http://jsfiddle.net/jfriend00/twu5n/
精彩评论