jQuery 使用 AJAX 加载方法加载一个 jsp 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14987391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:10:27  来源:igfitidea点击:

Load a jsp page using AJAX load method

jqueryajax

提问by Fahad

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 attached the image of the page where the jsp needs to be displayed and also my jsp code.. Please help!web page

大家好,我需要一些有关 ajax 加载方法的帮助。基本上,一旦用户选中单选按钮,我就需要使用 Ajax load() 在主页中显示一个 jsp。我附上了需要显示jsp的页面图片以及我的jsp代码..请帮忙!网页

   <div id="verification">
    <p align=center class="4f1_title" ><u>Verification Decision</u></p>
     <table border="0" cellpadding="0" cellspacing="0" align=center
width="100%">
<tbody>

    <tr>
        <td width="10%"></td>
        <td width="8%">Passed <input id="passed" type="radio"
            value="P" onclick="()"></td>
        <td colspan="2" width="8%">Pending <input id="pending"
            type="radio" value="H" onclick="()"></td>
        <td width="9%">True Hit <input id="failed" type="radio"
            value="F" onclick="()" disabled></td>
        <td width="13%">Parcel Returned <input id="returned"
            type="radio" value="S" onclick="()"></td>
        <td width="23%">Referred to Law Enforcement <input id="law"
            type="radio" value="L" onclick="()"></td>
        <td width="8%">Retired <input id="retired" type="radio"
            value="R" onclick="()" disabled></td>
              <td width="12%">Return Reason <input id="ac" 
              type="radio" value="C" onclick="()"></td>
         <td width="10%"></td>
    </tr>
         </tbody>
          </table>
         </div>
            <br>

                     <div align=center>
                <a href="javascript:handleClick()"><u>
              <div id='showhidecomment'>Show Comments</div></u></a>
              <div id='chkcomments'>
               </div>
    </div>
     <br>

采纳答案by Gabriele Petrioli

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

并将您的 html(链接部分)更改为

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

Since divelements are invalid inside aelements.

由于div元素在a元素内部无效。



updatefor comment

更新评论

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

我会data-url为这些元素添加一个自定义属性(以指定要加载的页面

<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 );
    }
});

回答by uv_man

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

如果你只是想 ajax 加载一个页面,你可以使用 jquery 加载:http: //api.jquery.com/load/

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

还有 jquery 获取: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.

如果您还没有,您可能需要添加对 jquery 库的引用。请参考下面的示例代码。

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

简单页面显示“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

带按钮的页面。当您单击按钮时,它使用 jquery 加载将上面的页面加载到 div 容器中:http: //jsbin.com/ubitat/1/edit