简体   繁体   中英

Can someone explain this javascript function?

I have this javascript function I am trying to understand. I don't know if it has been implemented in jquery.

It is a function that is related to a button. When the button is clicked on it should display on the screen that vote has been cast. Can you explain to me the purpose of 'id' and 'nb' in this function and what each line does. I presume the fourth line takes the user to vote_yes.php of the base url if they click on the button? $lang189 is a variable saying vote collected and output between the literal braces is html and javacript, with code outside the braces php.

  {literal}
    function VOTEYES(id,nb) {
        $('#vote'+id).css('display','none');
        $.post("{/literal}{$baseurl}/vote_yes.php{literal}",{"id":id,"nb":nb},function(html) {
            $('#vote'+id).html('{/literal}{$lang189}{literal} ('+html+')').fadeIn();
        });
    }{/literal}

I am familar with PHP not no javascript experience.. I don't want to learn the whole language to understand how this vote button works, please help. Cookies are involved.

Thanks.

  1. A function VOTEYES is defined that takes 2 arguments id and nb
  2. It finds an element with an ID of 'vote'+id, so if id was 2 it would be vote2
  3. It hides that element making it non-clickable.
  4. It does an ajax request, POST call to whatever $baseurl , the root domain I suppose, is and appends vote_yes.php to it
  5. It passes the id and nb parameters in the POST request.
  6. Then it takes the html from that vote_yes.php page and appends it to the vote2 element by fading it in.

The function is simply making a call to a page with the URL:

{/literal}{$baseurl}/vote_yes.php{literal}

And passing in the following parameters via "POST":

"id": id
"nb": nb

and using jQuery to fade in the response from the page.

Yes, it is using jQuery. It appears that the id is being used to determine which element with the id of voteID to use. I'm not quite sure what the nb is used for because it is simply passed as a POST variable to the vote_yes.php page in the AJAX. I would investigate that page for more info.

我相信函数内的第一行隐藏了按钮,因此用户无法再次单击它。

Will send id and nb to vote_yes.php. response output will be on (html) variable. now with $('#vote'+id).html(xxx) will set that the response received will be content of another html element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM