开发者

what is the meaning of $(this) in javascript

开发者 https://www.devze.com 2023-02-01 07:36 出处:网络
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\"/>

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( $(thi开发者_运维问答s).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?


$(this) refers to the HTML element of the calling function.


$ is a shorthand for the jQuery function. This will only work if you have the jquery library loaded in your page.


in your first example, $(this).previous() is going to refer to the input, so if you want to change your function, try

<script type="text/javascript" >
   function disable_pop_up(){
     if (edit==true)
       new CalendarDateSelect( $("#start_date_1"), {popup:'force', year_range:10} );
     else
       return false;
   }
</script>

p.s. the $ function, is shorthand for the main jQuery function

0

精彩评论

暂无评论...
验证码 换一张
取 消