Call jquery from code behind

后端 未结 3 827
夕颜
夕颜 2021-02-15 13:06

Hi i have a jquery function which executes when a button is clicked, i also need to execute this function from my code behind based on whether an Item has a comment attached to

相关标签:
3条回答
  • 2021-02-15 13:51

    try this

    Page.ClientScript.RegisterStartupScript(typeof(String), btnID,"$('.commentsnr').live("click", function () {
    $li = $(this).closest('li');
    $li.find("#commentload").slideToggle(300);});", True);
    
    0 讨论(0)
  • 2021-02-15 13:57

    Not sure exactly what you are asking, but I'm guessing its just how to manually call this method..?

    $(selector).click();
    

    The problem you will have is that its not an ID, its a class. So in my example you'd have to update the selector to find the exact commentsnr that you want to 'click'

    0 讨论(0)
  • 2021-02-15 13:59

    You can do this, but it will only be executed when the page is delivered or you receive a Postback.

    See ClientScriptManager.RegisterStartupScript for documentation.

    string jquery = "$('.commentsnr').live(\"click\", function () {$li = $(this).closest('li');$li.find(\"#commentload\").slideToggle(300);});"
    
    ClientScript.RegisterStartupScript(typeof(Page), "a key", 
                 "<script type=\"text/javascript\">"+ jquery +"</script>"
                 );
    
    0 讨论(0)
提交回复
热议问题