jQuery/Javascript如何使用Get解析HTML [英] JQuery/Javascript how to Parse HTML using a Get
问题描述
我有以下工作代码.这将显示一个下拉列表,并且还提取了要显示的html文件:
I have the following working code. This displays a drop down and also fetches a html file to be displayed:
$.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.
然后我需要它来获取与选择相关的html文件,例如,如果他们选择"FML"选项,那么如果他们选择"GBP",它将得到html文件"FML_shares.html",那么它应该得到"GBP_shares" .html",最后,如果该选择没有任何与之相关的html文件,则应显示错误,例如无此文件"等.
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.
只是使其变得更复杂一点,所以我不需要整个文件.该文件中有一个表,我想从表的第一行获取数据的前五列,并单独显示这些数据.
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.
感谢您的协助,一段时间以来我一直在寻找解决方案,但没有任何成功,而且我的JQuery/Javascript知识非常基础! (过去我已经用PHP做过类似的事情,但这不是这里的选择)
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! (I've done something similar with PHP in the past but that's not an option here)
推荐答案
似乎您只需要绑定到要创建的下拉列表的.change
事件即可进行提交,可以使用.get
进行检索.您可以使用jQuery解析html.它做得很好:
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
. You can use jQuery to parse the 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 */ });
});
此代码未经测试,但希望您可以按照以下示例进行操作.还请注意GET
缓存.
This code is untested, but hopefully you can follow the example. Also beware of GET
caching.
这篇关于jQuery/Javascript如何使用Get解析HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!