javascript中$(this)的含义是什么
我有以下用于日历弹出窗口的代码,该代码工作正常,
<input type="text" value="12/01/2010" readonly="readonly" name="start_date_1" id="start_date_1" disabled="disabled"/>
<img src="/images/calendar_date_select/calendar.gif" onclick="new CalendarDateSelect( $(this).previous(), {popup:'force', year_range:10} );" class="calender_image" alt="Calendar"/>
但我想要的是我的日历弹出窗口最初将保持禁用状态,只有在单击“编辑”按钮后,它才应该打开。我使用 disabled="disabled"
但由于 popup:'force'
它不起作用
所以我编写了以下代码
<script type="text/javascript"
function disable_pop_up(){
if (edit==true)
new CalendarDateSelect( $(this).previous(), {popup:'force', year_range:10} );
else
return false;
}
</script>
<input type="text" value="12/01/2010" readonly="readonly" name="start_date_1" id="start_date_1" disabled="disabled"/>
<img src="/images/calendar_date_select/calendar.gif" onclick="disable_pop_up()" class="calender_image" alt="Calendar"/>
当然 JavaScript 按预期失败了,所以我的问题是我应该写一个 disable_pop_up()
来完成它吗?
*已编辑*
我的问题通过发送 $(this)
作为参数得到解决
function disable_pop_up(cal, id){
disable = document.getElementById(id).disabled
if (disable==true)
return false;
else
new CalendarDateSelect( cal.previous(), {popup:'force', year_range:10} );
}
onclick="disable_pop_up($(this), 'start_date_1'"
但我的问题仍然相同为什么我不能写类似 $( "#start_date_1")
在我的 JavaScript 函数中?
I have following code for Calendar Pop-Up which is working fine
<input type="text" value="12/01/2010" readonly="readonly" name="start_date_1" id="start_date_1" disabled="disabled"/>
<img src="/images/calendar_date_select/calendar.gif" onclick="new CalendarDateSelect( $(this).previous(), {popup:'force', year_range:10} );" class="calender_image" alt="Calendar"/>
but what I want is my calendar pop up will remain disabled initially and after clicking on Edit button only it should get open. I use disabled="disabled"
but it's not working due to the popup:'force'
So I write following code
<script type="text/javascript"
function disable_pop_up(){
if (edit==true)
new CalendarDateSelect( $(this).previous(), {popup:'force', year_range:10} );
else
return false;
}
</script>
<input type="text" value="12/01/2010" readonly="readonly" name="start_date_1" id="start_date_1" disabled="disabled"/>
<img src="/images/calendar_date_select/calendar.gif" onclick="disable_pop_up()" class="calender_image" alt="Calendar"/>
Of course JavaScript fails as expected, so my question is what should I write in a disable_pop_up()
to accomplish it?
*EDITED *
My problem is get solved by sending $(this)
as an argument
function disable_pop_up(cal, id){
disable = document.getElementById(id).disabled
if (disable==true)
return false;
else
new CalendarDateSelect( cal.previous(), {popup:'force', year_range:10} );
}
onclick="disable_pop_up($(this), 'start_date_1'"
But still my question remains same why can't I write something like $("#start_date_1")
in my JavaScript function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$(this) 指的是调用函数的 HTML 元素。
$(this) refers to the HTML element of the calling function.
$
是jQuery
函数的简写。仅当您的页面中加载了 jquery 库时,这才有效。$
is a shorthand for thejQuery
function. This will only work if you have the jquery library loaded in your page.在第一个示例中,
$(this).previous()
将引用输入,因此如果您想更改函数,请尝试p.s. $ function 是 jQuery 主函数的简写
in your first example,
$(this).previous()
is going to refer to the input, so if you want to change your function, tryp.s. the $ function, is shorthand for the main jQuery function