2017-06-15 58 views
0

每當AJAX調用的頁面應該被重新加載,但形式各領域不應該獲得的頁面加載如何使用清除表單域重新加載ajax調用頁面?

我的形式清除

<form method='post'> 
<input type='text' placeholder='product'/> 
<input type='number' placeholder='cost'/> 
<input type='number' placeholder='tax'/> 
<button type='button' id="cust_tax" data-toggle="collapse" data-target="#cust_tax">Custom tax</button> 
<input type='number' placeholder='new tax' id="cust_tax" class="collapse"/> 
<input type='button' class='apply_tax'>New tax add</button> 
<input type='submit'/> 
</form> 

在自定義的稅收用戶點擊有一個Ajax調用將new_tax保存在數據庫中,並自動添加到列表中。

我的AJAX

$(".tax_apply").click(function() { 
var tax_name = document.getElementById("tax_name").value; 
var tax_value = document.getElementById("tax_value").value; 
ajaxPost('/myurl',{'user':user,'tax_name':tax_name,'tax_value':tax_value},function(result){ 
        alert('new tax added'); 
       //reload page 
        }); 
}); 

現在每當AJAX調用和用戶添加新的稅種,應該重新加載,他已經進入不應該得到清除

+2

爲什麼_it應該重新加載page_? – Rayon

+0

由於一些支持問題無法解決 –

+2

我認爲你應該發佈/詢問/解決這個問題,而不是問這個補丁! – Rayon

回答

3

所以,你可以做的是附加頁面,但表單字段您的表單字段的事件偵聽器,將輸入的內容保存到localStorage中的對象,然後在頁面加載時,您可以檢索該內容並將其放回到各自的字段中。

下面是你可能做的一個例子。

$(myCollectionOfFields).keyup(function(){ 
    $(myFormFields).each(function(){  
     var id = $(this).attr('id'); 
     var value = $(this).val(); 
     localStorage.setItem(id, value); 

    }); 
}); 

$(document).ready(function() { 
    $(myFormFields).each(function(){  
     var id = $(this).attr('id'); 
     var value = localStorage.getItem(id); 

     $(this).val(value); 

    }); 
}); 

我覺得我必須指出的是,這種感覺就像一個容易出錯的解決方案(例如,並不是所有的用戶允許的localStorage),而且我會解決你所提到的,而不是使用這種方法的後端問題。

但它可以做理論上的工作。

相關問題