Load a jsp page using AJAX load method

前端 未结 2 1717
栀梦
栀梦 2020-12-15 01:16

Hello everyone I need some help with ajax load method. Basically I need to use Ajax load() to display a jsp within the main page once user checks the radio button. Ive att

相关标签:
2条回答
  • 2020-12-15 01:37

    If you just want to ajax load a page you can use the jquery load: http://api.jquery.com/load/

    There's also the jquery get: http://api.jquery.com/jQuery.get/

    You can find a simple example in the documentation.

    Update:

    You may need to add a reference to the jquery library if you haven't already. Please refer to the sample code below.

    Simple page displays 'hello world' : http://jsbin.com/ivinuw/1/

    Page with button. When you click button it uses jquery load to ajax load the page above into a div container: http://jsbin.com/ubitat/1/edit

    0 讨论(0)
  • 2020-12-15 01:52

    Try

    $(function() { // when DOM is ready
        $("#showhidecomment").click(function(){ // when #showhidecomment is clicked
            $("#chkcomments").load("sample.jsp"); // load the sample.jsp page in the #chkcomments element
        }); 
    });
    

    And change your html (the link part) to

    <div>
        <div id='showhidecomment'>Show Comments</div>
        <div id='chkcomments'></div>
    </div>
    

    Since div elements are invalid inside a elements.


    update for comment

    I would add a custom data-url attribute to those elements (to specify the page to load)

    <input id="passed" type="radio" value="P" data-url="some-page.jsp" />
    <input id="law" type="radio" value="L" data-url="some-other-page.jsp" />
    <input id="ac" type="radio" value="C" data-url="some-third-page.jsp" />
    

    and then apply a single handler to them

    $('#verification').on('change','input[type="radio"][data-url]', function(){
        if (this.checked){
            var url = $(this).data('url');
            $("#chkcomments").load( url );
        }
    });
    
    0 讨论(0)
提交回复
热议问题