【问题标题】:Loading html content based on radio values using jquery load使用 jquery load 根据单选值加载 html 内容
【发布时间】:2015-02-08 10:15:10
【问题描述】:

我正在使用 jquery 加载函数编写一个脚本,但我遇到了一个菜鸟问题..

我有 2 个 html 文件,

  1. testload.html Pastebin file

  2. html 文件的名称因 testload.html 中给出的输入值而异,但出于演示目的,我创建了 test.html 并在输入字段中输入“test”进行测试。

演示 test.html - Test.html

现在的问题是,当用户单击 testload.html 文件中的单选按钮时,应该从 test.html 中仅检索基于 id 的 div 的内容,但我会获取所有 div 的内容。

test.html 中 div 的 id 与 testload.html 中单个单选按钮的值相同

总结

用户在 testload.html 的输入字段中输入文本(演示目的:测试)

选择其中一个

test1 radio => 显示 #1 of test.html

test2 radio => 显示 #2 of test.html

test3 radio => 显示 #3 of test.html

问题:我一次性获取所有内容,而不是所需内容。

希望在这里得到一些帮助..

更新:

没关系.. 我的一个小错误。它现在固定了。问题是我错过了加载函数的连接。我将脚本行更改为..

    $('input[type="radio"]').click(function() {
        var test = $(this).val();        
        var url = $('#page').val();
        url = url +".html #";
        $('#getcontent').load(url + test);

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    我想我已经为您想出了一个简单的解决方案。

     $('#radio1').click(function() {
           $('#res').html("");
           $('#res').load("index.html #target"); //where Target stands for the DIV inside the html file
        });
    
    
        $('#radio2').click(function() {
           $('#res').html("");
           $('#res').load("index.html #target");
        });
    

    如果对您有帮助,请告诉我,祝您编写愉快!

    更新

    如果您想使用 var 而不是指定正确的 div ID 或您可以使用的 html 文件

     $('#res').load(htmlVar".html #"+divVar);
    

    但由于您使用的是 ID,因此您必须在 div 的 ID 之前使用 #。

    欲了解更多信息,请访问:http://api.jquery.com/load/(正在加载页面元素)。

    【讨论】:

    • 感谢您的快速响应,但加载函数中的 index.html 会根据 testload.html 中的用户输入文本而有所不同。所以我无法对文件名进行硬编码。而且还有很多单选按钮,所以我不能为每个功能编写单独的功能...... :(
    • 您可以指定例如您想要所有单选按钮 $('input') 并在 click 函数中使用 $(this) 单独获取每个单选按钮的 val。
    • 可以通过变量指定html文件,只要做.load(varHere+".html #"+divVar);例如。
    • 是的,我很久以前就想到了这一点,并完成了脚本。请找到已编辑的帖子。
    • 编辑了具体问题的答案!
    【解决方案2】:

    请参阅:在http://api.jquery.com/load/ 中加载页面片段

    $( "#result" ).load( "ajax/test.html #container" );
    

    【讨论】:

    • 请阅读全文..正如我所说的 html 文件的名称各不相同,不能硬编码。
    猜你喜欢
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多