javascript序列化问题 [英] javascript serializing question
问题描述
我正在使用以下内容...
I'm using the following...
// (jquery / pseudo code)
var base = $('form[name="gs"]').attr('action');
var params = $('form[name="gs"]').serialize();
var url = base + '?' + params;
我有一个侦听器,当按下某个键时,该侦听器会调用此函数.它在搜索栏中获取查询.但是,如果在执行后在搜索框中键入more,则返回undefined的结果.我该如何解决?
I have a listener which calls this function when a key is pressed. It's grabbing a query in a search bar. However, if type more in the search box after it has already executed once it returns a result of undefined. How would I fix this?
推荐答案
在函数外部声明全局变量:
Declare global variables outside the function:
var globalBase, globalParams;
然后在初始时间缓存值并相应地更新它们:
Then cache the values the initial time and update them accordingly:
globalBase = (globalBase === void(0)) ?
$('form[name="gs"]').attr('action') :
globalBase;
globalParams = (globalParams === void(0)) ?
$('form[name="gs"]').serialize() :
globalParams;
var url = globalBase + '?' + globalParams;
如果您需要根据按键时输入的内容来更新基本或参数,则可以将$(yourInput).val()
.
If you need to update the base or params based on what is typed on key press, then you can replace the else statements with something like $(yourInput).val()
.
这篇关于javascript序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!