简体   繁体   English

Ajax:使用onclick jQuery连续刷新div

[英]Ajax: refresh div continuously with onclick jQuery

I have been trying refresh the div contents after button click but it happen only one time and need to refresh the page to click the button again to refresh the content. 我一直在尝试单击按钮后刷新div内容,但是这种情况仅发生一次,因此需要刷新页面以再次单击按钮来刷新内容。 I'm new to AJAX, please help me to solve this issue. 我是AJAX的新手,请帮助我解决此问题。

Here is my HTML: 这是我的HTML:

<li id="personal-info" class="personal-info-wrapper">
    <div class="profile_content" id="personal-info-contents"> 
      **more codes to follow....**
      **at last**
    <tr class="pro-info-set">
        <td class="info-group-left">
            <input class="form-control" type="text" id="js_personal_title" name="js_personal_title">
        </td>
        <td class="info-group form-inline">
            <input class="form-control" type="text" id="js_personal_desc" name="js_personal_desc">
            <input id="submit_person" class="form-control" type="button" value="Add">
            <label id="submit_person_msg" value="Add"></label>
        </td>                                       
    </tr>
       <?php }  ?>
</table>

Here is my JS 这是我的JS

$("#submit_person").click(function(e){
    e.preventDefault();
    js_personal_title = $("#js_personal_title").val();
    js_personal_desc= $("#js_personal_desc").val();
    if (js_personal_title !== null && js_personal_title !== "") {
        if (js_personal_desc !== null && js_personal_desc !== "") {
            var datastr = 'js_personal_title='+js_personal_title + '&js_personal_desc='+js_personal_desc; 
            $.ajax({
                type: "POST",
                url: "<?php echo base_url().'index.php/Jobseeker/add_personal' ?>",
                data: datastr,
                success:function(e) {
                    //e.preventDefault();
                    $(".personal-info-wrapper").load(location.href  + " #personal-info-contents");
                }
            });
        } else { alert('Please enter the decription');}
    } else { alert('Please enter the title');}
return false;
});

You are clearing out all the elements inside the li element and replacing them with new ones. 您正在清除li元素内的所有元素,并用新元素替换它们。 Your old listeners are gone. 你的老听众都不见了。 You'll have to reattach the listeners or use delegation. 您必须重新附加侦听器或使用委派。 For example, change this: 例如,更改此:

 $("#submit_person").click(function(e){

For this: 为了这:

 $('.personal-info-wrapper').on('click','#submit_person', function(e) {

This assuming .personal-info-wrapper never gets replaced. 假设.personal-info-wrapper永远不会被替换。

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

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