我尝试使用JavaScript或jQuery通过刷新按钮更改页面中的内容。我想更改表中的内容,但我使用的任何命令都会删除表中的部分内容并将新内容保留为白色。
到目前为止,我'我们尝试了change()
,replaceWith()
,load()
,text()
,但所有这些都删除了表格的一部分。使用document.write()
,它会在新页面中打印。
我可以使用任何命令来替换已经写好的新内容表吗?
更新
代码
<script>
var result = [];
var k = 0;
var jsonObject = JSON.parse(eventsJson);
$(document).ready(function(){
$("#priceInfoB").on("click", function(){
//alert(jsonObject.events.event[0].name);
for (i=0;i<4;i++){
if (jsonObject.events.event[i].isFree == "true"){
result[k] = jsonObject.events.event[i].name;
k++;
}
}
$("#events").text(result[0] + result[1]);
$('#myTable').remove();
});
});
<table id="myTable">
<tr class="head">
<th></th>
<th>New York</th>
<th>Chicago</th>
<th>San Francisco</th>
</tr>
<tr>
<th>BLABLA</th>
<td>Sat, 4 Feb 2012<br />11am - 2pm</td>
<td>Sat, 3 Mar 2012<br />11am - 2pm</td>
<td>Sat, 17 Mar 2012<br />11am - 2pm</td>
</tr>
var eventsJson='{"events":{"event":[{"id":"1","name":"A Poetic Perspective","isFree":"true","locations":[{"location":"New York","eventDate":"2015-05-02","eventTime":"14:00"},{"location":"Chicago","eventDate":"2015-05-01","eventTime":"14:00"},{"location":"San Francisco","eventDate":"2015-06-01","eventTime":"15:00"}],"descr":"Vivamus elementum, diam eget ullamcorper fermentum, ligula libero euismod massa, quis condimentum tellus lacus sit."},{"id":"2","name":"Walt Whitman at War","isFree":"false","locations":[{"location":"New York","eventDate":"2015-07-02","eventTime":"14:00"},{"location":"Chicago","eventDate":"2015-07-01","eventTime":"14:00"},{"location":"San Francisco","eventDate":"2015-08-01","eventTime":"15:00"}],"descr":"Donec convallis eu metus eget dictum. Etiam non lobortis dui."},{"id":"3","name":"Found Poems & Outsider Poetry","isFree":"false","locations":[{"location":"New York","eventDate":"2015-06-02","eventTime":"11:00"},{"location":"Chicago","eventDate":"2015-07-01","eventTime":"14:00"},{"location":"San Francisco","eventDate":"2015-06-01","eventTime":"15:00"}],"descr":"Ut fermentum, elit vel iaculis viverra, dui libero ultrices nibh, ut ornare."},{"id":"4","name":"Natural Death: An Exploration","isFree":"true","locations":[{"location":"New York","eventDate":"2015-05-02","eventTime":"14:00"},{"location":"Chicago","eventDate":"2015-05-01","eventTime":"14:00"},{"location":"San Francisco","eventDate":"2015-06-01","eventTime":"15:00"}],"descr":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent aliquet urna ut tortor consequat."}]}}';
答案 0 :(得分:1)
这将从提供的JSON ...
更新表http://jsfiddle.net/ArchersFiddle/eemxvu0z/
<强> HTML 强>
<table id="myTable">
<tr class="head">
<th></th>
<th>New York</th>
<th>Chicago</th>
<th>San Francisco</th>
</tr>
<tr>
<th>BLABLA</th>
<td>Sat, 4 Feb 2012<br />11am - 2pm</td>
<td>Sat, 3 Mar 2012<br />11am - 2pm</td>
<td>Sat, 17 Mar 2012<br />11am - 2pm</td>
</tr>
</table>
<强>的Javascript 强>
var data = JSON.parse(eventsJson);
function showEvent(id) {
for (var i in data.events.event) {
if (data.events.event[i].id == id) {
var thisEvent = data.events.event[i];
var head = "<tr class=\"head\"><th></ht>";
var row = "<tr><th>" + thisEvent.name + "</th>";
for (var l in thisEvent.locations) {
var thisLocation = thisEvent.locations[l];
head += "<th>" + thisLocation.location + "</th>";
row += "<td>" + thisLocation.eventDate + "<br/>" + thisLocation.eventTime + "</td>";
}
head += "</tr>";
row += "</tr>";
$("#myTable").html(head + row);
}
}
}
showEvent(4); // for example
基本上,只需从提供的数据重建表的html,然后使用$("#myTable").html(html);
进行更新。我希望您将使用事件(例如选择值更改)来触发表更新,因此只需使用showEvent(id);
函数调用来更新它。