简体   繁体   English

jQuery / Javascript如何使用Get解析HTML

[英]JQuery/Javascript how to Parse HTML using a Get

I have the following working code. 我有以下工作代码。 This displays a drop down and also fetches a html file to be displayed: 这将显示一个下拉列表,并且还提取了要显示的html文件:

   $.getJSON('json/shares.json', function(data) {
      var items = [];

      $.each(data.Shares, function(key, val) {
        items.push('<option id="' + val.shareID+ '">' + val.shareID+ '</option>');
      });

      $('<select/>', {
        'id': 'shares',
        html: items.join('')
      }).appendTo('#shares');
    });
    </script>

    <script type="text/javascript">

    $.get('lon_shares.html', function(data){
        $(data).appendTo('#shares');
    });

    </script>   

I need to amend this to a few extra things. 我需要对此进行一些补充。

Firstly, I need the drop down to auto submit when a choice is made. 首先,我需要下拉菜单以在做出选择时自动提交。

I then need it to get the html file relevant to the choice, for example if they choose the "FML" option it will get the html file "FML_shares.html" if they choose "GBP" then it should get "GBP_shares.html" and finally if the choice doesn't have any html file related to it then an error should be displayed such as "no such file" etc. 然后,我需要它来获取与选择相关的html文件,例如,如果他们选择“ FML”选项,则他们将获得html文件“ FML_shares.html”;如果他们选择“ GBP”,那么它将获得“ GBP_shares.html”最后,如果该选择没有任何与之相关的html文件,则应显示错误,例如“无此文件”等。

Just to make it a little more complex, I don't want the whole file. 为了使其更加复杂,我不需要整个文件。 The file has a table in it and I want to get the data from the first row of the table, for the first five columns of data and display those alone. 该文件中有一个表,我想从表的第一行获取数据的前五列,并单独显示它们。

Thanks for any assistance, I've been searching for a solution for a while without any success and my JQuery/Javascript knowledge is very basic! 感谢您的协助,一段时间以来我一直在寻找解决方案,但没有成功,我的JQuery / Javascript知识非常基础! (I've done something similar with PHP in the past but that's not an option here) (过去我已经用PHP做过类似的事情,但这不是这里的选择)

Well for the first point you could just run 首先,您可以运行

$('#shares').on('change', function(){
    var val = $(this).val();
    //submit another ajax request with this value, and get the relevant page, then you'd just need to parse it for the appropriate content from that page.
});

If you want to parse the returned page and get only part of this we'd need to know the markup structure. 如果您想解析返回的页面并仅获取其中的一部分,我们需要知道标记结构。

Seems like you just need to bind to the .change event of the dropdown you are creating to do the submission, which you can retrieve with .get . 似乎您只需要绑定到要创建的下拉列表的.change事件即可进行提交,您可以使用.get进行检索。 You can use jQuery to parse the html. 您可以使用jQuery解析html。 It does a nice job of that: 它做得很好:

.appendTo('#shares')
.change(function () {
   $.get($(this).val() + '_shares.html)
      .done(function (html) {
         var $table = $(html).find("table tr:first td").slice(0,5);
      })
      .fail(function () { /* no such file */ });
});

This code is untested, but hopefully you can follow the example. 该代码未经测试,但是希望您可以按照以下示例进行操作。 Also beware of GET caching. 还要提防GET缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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