2012-05-18 48 views
1

朋友你好形式我有一個<input type="text" id="tempID" />元素在我的形式增加值的文本框歷史沒有提交使用JavaScript或Jquery的

我也有我的形式<input type="button" onclick="doSomething()" />元素。

我想在用戶單擊按鈕時將文本框的值添加到文本框歷史記錄中。

我正在使用Jquery ajax處理請求。所以我必須用javascript或Jquery來完成。

是否可以使用javascript/Jquery爲特定<input type="text" />元素的歷史記錄添加值?

+0

NOP粘貼在,沒有任何的爲元素的歷史記錄添加值。但是,每次用戶點擊時,您都可以維護一個特殊的「數組」和「推」值,並在需要時將其拉回。 –

+0

@Joy但是當用戶刷新頁面時,數組中的值將會丟失。有沒有其他的.. ?? – shashwat

+0

然後,如果您希望在請求之間持續存在,那麼您已將其添加到cookie中。你可以使用這個插件https://github.com/carhartl/jquery-cookie –

回答

2

這裏是如何使用做HTML5 LocalStorage

$("#tempID").autocomplete({ 
    source: function(req, resp) { 

    var temp = localStorage.getItem('custom_history'); 

    var data = JSON.parse(temp); 
    resp(data); 

} 
}); 


$('#tempID').on('blur', function() { 
    var temp = localStorage.getItem('custom_history'); 

    var data = JSON.parse(temp); 
    if($.trim(this.value).length > 0) 
     data.push(this.value); 

    localStorage.setItem('custom_history', JSON.stringify(data)); 

});

我在做什麼是將值設置爲HTML5本地存儲當用戶離開輸入字段,點擊其他地方。

然後檢索並設置爲jQuery UI自動完成的源。

這裏是工作提琴http://jsfiddle.net/joycse06/EBduF/173/

輸入一些值。點擊其他地方。再次點擊並添加其他值。刷新小提琴,並開始輸入其中一個和自動完成將顯示出來。

UPDATE

根據他的意見,後來聊天的最終代碼,他需要的就是這個,我如果可能別人後面

// if we dont set this line then ff will return null, and null.length will throw an error 
if(!localStorage.getItem('custom_history')) 
     localStorage.setItem('custom_history',''); 
$("#test").autocomplete({ 
    source: function(req, resp) { 
     var term = req.term; 
     var temp = localStorage.getItem('custom_history'); 
     var data = []; 
     if(temp.length > 0) 
      data = JSON.parse(temp); 
     var intRegex = /^\d+$/; 
     data = $.map(data,function(val){ 
       if(intRegex.test(val)){ 
        if(val.indexOf(term) != -1) 
         return val; 
        else 
         return null; 
       } 
       else 
        return null; 
      }); 
     resp(data); 

    } 
}); 


$('#save').on('click', function() { 
    var temp = localStorage.getItem('custom_history'); 

     var data = []; 
     if(temp.length > 0) 
     data = JSON.parse(temp); 
     var value = $.trim($('#test').val()); 
     if(value.length > 0){ 
     if($.inArray(value,data) != -1){ 
      //alert('Duplicate'); 
      return; 
     } 

     } 
     data.push(value); 
     localStorage.setItem('custom_history', JSON.stringify(data)); // set item to localStorage 
}); 
+0

你的小提琴錯誤是錯誤的是數據爲空。我使用的是火狐12.0 @Joy – shashwat

+0

檢查它不能在小提琴中工作http://joynag.net/demos/localstorage/嘗試以相同的方式,輸入內容,點擊其他地方,點擊返回並添加新的東西然後刷新並開始輸入 –

+0

現在讓我知道它是否適用於您? –

0

可以使用localStorage像以下:

var arr = []; 
$('input#tempID').on('click', function() { 
    arr.push(this.value); 
    localStorage.setItem('history', JSON.stringify(arr)); // set item to localStorage 
}); 

要檢索值嘗試,

var temp = localStorage.getItem('history'); 

if(retarr) { // checking that data it stored in localStorage or not, if not exists temp = null 
    var allHistories = JSON.parse(temp); // now you have history 
    console.log(allHistories); 
} 

我認爲你需要像autocomplete

+1

我不認爲這是他所要求的。 –

+0

我知道如何在點擊按鈕時發出ajax請求。我想知道 '在文本框歷史記錄中添加值而不提交使用javascript或Jquery的表單作爲問題標題。 – shashwat

+0

@thecodeparadox,但是當用戶開始在文本框內輸入或雙擊時(空時),我將如何顯示它們。請參閱任何好文章,如果你有一個 – shashwat