简体   繁体   English

如何在AJAX加载页面中更改html

[英]How to change html in a AJAX loaded page

I am using AJAX to load a PHP page into a div called 'DIV1' In there it creates a part of table and in there is another div called 'directions' I want to be able to change the contents of the 'directions' div but cant seem to find it? 我正在使用AJAX将PHP页面加载到称为“ DIV1”的div中,在其中创建表的一部分,在另一个div中称为“ directions”,我希望能够更改“ directions” div的内容,但是似乎找不到它? Is it because its dynamic? 是因为它动态吗? I have tried several things and will show example, but where am I going wrong? 我已经尝试了几件事,并且将显示示例,但是我在哪里出错呢?

The code for the AJAX load is AJAX加载的代码是

$(document).ready(function(){
    $(".mainstuff button").click(function(){
        $('#loader').show();
        status = $(this).attr("data-name");
        var new_url = "get_job_details3.php?job_id="+status;
        //alert(status);
        $("#div1").load(new_url);           
    });
});

The part of the PHP that generates the table where the 'directions' div is like so : PHP的生成表的部分,其中“ directions” div如下所示:

echo "<input style='display:none' id='p_lat' type='text' value='".$p_lat."'><input style='display:none' id='p_lng' type='' value='".$p_lng."'><input style='display:none' id='d_lat' type='text' value='".$d_lat."'><input style='display:none' id='d_lng' type='' value='".$d_lng."'><table class='mainTable'>
<tbody>
<tr>


<td width='50%' style='vertical-align: top'><li>Vehicle required : <B>".$vehicle_required."</B></li>$r<li>Pick up time : <B>".$new_date."</B></li><li>Pick up time : <B>".$p_time."</B></li><li>Pick up address : <B>".$p_address."</B></li><li>Destination address : <B>".$d_address."</B></li><li>Return date : <B>".$d_date_new."</B></li><li>Return time : <B>".$d_time."</B></li><li>Number of passengers : <B>".$pax."</B></li><li>Estimated distance : <B>".$est_dist."</B></li><li>Estimated time : <B>".$est_time."</B></li><li></li><div id='mymap'><li><button id='show_map' style='margin-right:10px' type='button' class='btn btn-success'><span class='glyphicon glyphicon-map-marker'></span> SHOW ON MAP </button><button id='show_directions' data-toggle='collapse' data-target='#directions' type='button' class='btn btn-success'><span class='glyphicon glyphicon-map-marker'></span> SHOW DIRECTIONS </button><div id='directions' class='collapse'>Lorem ipsum dolor text....</div></li></td>
<td width='50%' id='description'><li>Purpose : <B>".$purpose."</B></li><li>Extra requirements : <B>".$list."</B></li><li>Notes : <B>".$notes."</B></li><li>Total current bids : <B>".$total_bids."</B></li><li>Current lowest bid : <B>£".$lowest_bid_price."</B></li><li>Your bids on this job : <B>".$your_bids."</B></li><li>Your current lowest bid : <B>£".$my_lowest_bid."</B></li><li>Make a new bid of : £<input id='bid_amount' type='text'><li><textarea id='extras' class='extras' placeholder='Enter any information you would like the customer to know'></textarea></li><li><button id='make_bid' data-name='".$job_id."' type='button' class='btn btn-bid'><span class='glyphicon glyphicon-pencil'></span> MAKE BID </button><button type='button' style='float:right' class='btn btn-success' onclick='closedown()'><span class='glyphicon glyphicon-remove-circle'></span> CLOSE </button></li></td>
</tr>
</tbody>
</table>";

and I am trying to update the content of the 'directions' div like so : 而且我正在尝试像这样更新'directions'div的内容:

directionsDisplay.setPanel(document.getElementById('#div1 .mainTable directions'));

I have tried everything from 'directions' to the above. 我已经尝试了从“方向”到上述所有内容。 What am I doing wrong? 我究竟做错了什么?

Thanks in advance 提前致谢

getElementById() doesn't take a css selector, as it name implies it takes just the id string. getElementById()不采用css选择器,因为它的名称暗示仅采用id字符串。 You might have meant to use querySelector() which does take a css selector. 您可能已经打算使用带有CSS选择器的querySelector() Note fixed your directions part to actually be a id selector. 注意将您的directions部分固定为实际上是一个ID选择器。

document.querySelector('#div1 .mainTable #directions')
//or just
document.querySelector("#directions")

But if you actually meant to use getElementById then just pass the element's id string: 但是,如果您实际上打算使用getElementById,则只需传递元素的ID字符串即可:

document.getElementById("directions")

Like Patrick mentions, the getElementById needs to be fixed. 就像Patrick提到的那样, getElementById需要固定。 But it also depends on when you run the code. 但这也取决于您何时运行代码。 In order to make sure you run the setPanel code after the server call is done, you can put it in the success callback of the .load() (which runs when the load is done); 为了确保在服务器调用完成后运行setPanel代码,可以将其放入.load()成功回调中 (在完成加载时运行)。

$("#div1").load(new_url, function () {
    // this code will run after the ajax is done
    directionsDisplay.setPanel(document.getElementById('directions'));
});

edit: 编辑:

I don't know what your setPanel method does, but document.getElementById('directions') should be able to get the inserted div after the load has run. 我不知道您的setPanel方法能做什么,但是document.getElementById('directions')应该能够在加载运行后获取插入的div。

If you just want to change its contents you could do: 如果只想更改其内容,则可以执行以下操作:

document.getElementById('directions').innerHTML = "some new text";

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

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