我将ASP.NET 3.5与残暴的UpdatePanel、jQuery.delegate()
和JavaScript对象文字结合使用。
我使用jQuery.delegate()
持久化事件处理程序,这些事件处理程序确实在PostBack之后触发,正如预期的那样。
问题在于存储在对象字面量属性中的jQueryinit()
对象,它们保留了在ASP.NET PostBack之前的值。
这是我的JavaScript:
SFAIC
SFAIC =
{
masterPages: { cs: "CS_", cp: "CP_" },
contentPages: { cs: "CSContent_", cp: "CPContent_" },
isSecurePage : false,
$body : undefined,
$container : undefined,
$updatePanel : undefined,
$textBox : undefined,
$errorLabel : undefined
};
SFAIC.init()
SFAIC.init = function ()
{
log( "--> SFAIC.init() fired" );
var self = this;
self.$body = $( "body" );
self.$container = self.$body.find( "#container" );
self.$updatePanel = self.getCSMasterElement( "UpdatePanel1", self.$container );
self.$textBox = self.getContentElement( "TextBox1" );
self.$errorLabel = self.getContentElement( "ErrorLabel1" );
self.$updatePanel.delegate( self.$textBox.selector, "blur", function ()
{
self.validateRequired( self.$textBox, self.$errorLabel );
});
self.$textBox.focus();
};
SFAIC.validateRequired()
SFAIC.validateRequired = function ( $element, $errorLabel )
{
if ( $element.val().length === 0 ) { $errorLabel.text( "Required" ); }
else { $errorLabel.text( "" ); }
};
鉴于上述情况,whenSFAIC.init()
被触发,self.textBox
被分配给它的 jQuery 对象。我想这就是我的困惑。我认为当您调用 时.val()
,它会立即返回该元素中的值。如果您调用服务器并更改控件的值,然后返回渲染元素的更改值,那么 jQuery 对象似乎不知道该更改?
我在这里错了吗?
我需要做些什么来保持对元素的实时引用,以便在页面回传后可以获得当前值?
每个@DaveLong 的编辑请求:
SFAIC.getCSMasterElement()
SFAIC.getCSMasterElement = function ( id, $context )
{
return SFAIC.getElement( SFAIC.masterPages.cs, id, $context );
};
SFAIC.getCPMasterElement()
SFAIC.getCPMasterElement = function ( id, $context )
{
return SFAIC.getElement( SFAIC.masterPages.cp, id, $context );
};
SFAIC.getContentElement()
SFAIC.getContentElement = function ( id, $context )
{
return SFAIC.getElement
(
( SFAIC.isSecurePage ) ? SFAIC.contentPages.cp : SFAIC.contentPages.cs,
id,
$context
);
};
SFAIC.getElement()
SFAIC.getElement = function ( page, id, $context )
{
selector = SFAIC.getElementIdSelector( page, id );
return ( selector )
? ( $context && $context.length )
? $context.find( selector )
: SFAIC.$body.find( selector )
: undefined;
};
SFAIC.getElementIdSelector()
SFAIC.getElementIdSelector = function ( page, id )
{
var prefix = SFAIC.getElementPrefix( page );
return ( prefix && id ) ? "#" + prefix + id : undefined;
};
SFAIC.getElementPrefix()
SFAIC.getElementPrefix = function ( page )
{
switch ( page )
{
case SFAIC.masterPages.cs : return SFAIC.masterPages.cs;
case SFAIC.masterPages.cp : return SFAIC.masterPages.cp + SFAIC.masterPages.cs;
case SFAIC.contentPages.cs : return SFAIC.masterPages.cs + SFAIC.contentPages.cs;
case SFAIC.contentPages.cp : return SFAIC.masterPages.cp + SFAIC.masterPages.cs + SFAIC.contentPages.cs + SFAIC.contentPages.cp;
}
};