【问题标题】:How to display the updated data in front end without refreshing page?如何在不刷新页面的情况下在前端显示更新的数据?
【发布时间】:2014-04-02 17:01:25
【问题描述】:

我想将编辑后的数据保存到数据库中,并在单击表单中的保存按钮时显示在前端。我做了第一部分,但我在不刷新页面的情况下努力在前端显示这个

<div class="leftbar_menu">
<div class="short_res_header">
<h3 style="margin-top: 10px;">
<span style="float:left;">About</span>
<span style="font-size:small;float: right;"><a data-edit-id="40322" data-clickmode="edit" class="pers" data-id="40322" style="display: inline;">edit</a></span><span style="font-size:small;float: right;"><a class="pers" data-close-id="40322" data-clickmode="close" data-id="40322" style="display: none;">close</a></span>             </h3>
</div>
<div class="leftbar_content">
<div data-id="40322" style="display: none;" class="about_toggle formarea"> 
<script type="text/javascript">
window.onLoad = imageRefreshBig();
$(document).ready(function(){
$('#profileshortresume').attr('maxlength','250');
$("#profileshortresume").css({
"max-width": "350px"
});
});
</script>

<div class="ajaxfiy_edit_profile">

<form enctype="multipart/form-data" method="post" action="http://10.0.1.21/firstplanet/dev/action/profile/edit/" onsubmit="return validateForm()" style="margin-left: 10px;" id="profile_edit_form" class="profile_edit_form_40322" name="profile_edit_form" novalidate="novalidate">
<div style="float:left;width:100%;" id="mypage_about" class=""><dl class="profileshortresume"><dt>About you<font style="color:#FF1800;font-size: 11px;padding-left: 3px;"> *</font><br></dt><dd>
<textarea title="" required="" id="profileshortresume" name="profileshortresume" class="input-textarea" maxlength="250" style="max-width: 350px;">good</textarea></dd></dl></div><input type="hidden" value="90d0e928746b45b9886292d1d0e08d5e" name="__elgg_token"><input type="hidden" value="1396426813" name="__elgg_ts"><input type="hidden" value="40322" name="cat_id"><input type="hidden" value="39242" name="custom_profile_type"><input type="hidden" value="job_1394777243" name="username">
<div class="subt_butt">
<a class="buttonS"><span><input type="button" value="Save" class="edit_save_button" style="text-align:center;"></span></a>
</div>
<br>

</form>
</div>
</div>
<div style="padding-right: 6px; display: block;" class="short_res" data-content-id="40322">
good    
</div>
</div>
</div>

//下面的脚本会在点击编辑时打开表单

<script type="text/javascript">

$('a.pers').click(function(){
var formid = $(this).attr('data-id');
var clickmode = $(this).attr('data-clickmode');

/*Closing All Open Div */
$('div.short_res').show();
$('div.formarea').hide();

if(clickmode == 'edit') {
$('a[data-clickmode=create]').show();
$('a[data-clickmode=edit]').show();
$('a[data-clickmode=close]').hide();

$('a[data-create-id='+formid+']').show();
$('a[data-close-id='+formid+']').show();
$('a[data-edit-id='+formid+']').hide();

$('div[data-id='+formid+']').show();
$('div[data-content-id='+formid+']').hide();         
}
if(clickmode == 'close') {

$('a[data-create-id='+formid+']').show();
$('a[data-edit-id='+formid+']').show();
$('a[data-close-id='+formid+']').hide();

$('div[data-id='+formid+']').hide();
$('div[data-content-id='+formid+']').show();                 
}
if(clickmode == 'create') {
$('a[data-clickmode=close]').hide();
$('a[data-clickmode=edit]').show();

$('a[data-close-id='+formid+']').show();
$('a[data-create-id='+formid+']').hide();

$('div[data-id='+formid+']').show();
$('div[data-content-id='+formid+']').hide();         
}   


});

// 我正在使用下面的脚本来更新数据库中的数据并显示在前端

$(".edit_save_button").click(function(){
var formid = $(this).parent().parent().parent().parent().attr("class");
var url = $("form."+formid).attr('action');
var data = $("form."+formid).serialize();
$(".thewire_previous_img").show();
var details = $(this).parent().parent().parent().parent().parent().parent().next().attr("class");
$.ajax({
type: "POST",
url: url,
data: data,
context: details ,
success:  function(data){
$(".formarea").hide();
$('div.short_res').show();
$('a[data-clickmode=close]').hide();
$('a[data-clickmode=edit]').show();
$(".thewire_previous_img").hide();

}
}); 

});

【问题讨论】:

    标签: javascript jquery html ajax


    【解决方案1】:

    如果您不希望页面刷新只是不使用&lt;form&gt; 元素和按钮作为&lt;input&gt;,这很简单, 只需使用JQuery .val() 获取元素值并获取&lt;button type="button"&gt; 元素的点击事件。 当您发送表单时,导航器始终会自动刷新页面或将您重定向到表单中引用的页面。

    【讨论】:

    • 我上面包含的表单只是示例,实际上很多字段都在表单中,包括复选框、下拉列表、文本框等......
    • 无论如何你应该用 JQuery 动态地获取这些数据,因为当你发送一个表单时页面会被刷新。
    猜你喜欢
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 2018-03-24
    • 2014-08-18
    • 1970-01-01
    相关资源
    最近更新 更多