change
以下是一个非常简单的发布/订阅,它创建一个存储的对象并使用表单输入的事件更新对象和页面元素
html:
<!-- table, match "data-bind" to form input name -->
<td data-bind="fname" class="subscriber">Foo</td>
<td data-bind="lname" class="subscriber">Bar</td>
<!-- form -->
<input type="text" name="fname" class="data_source" />
JS:
/* declare empty object */
var storedData = {};
var $subscribers=$('.subscriber');
/* this event triggered when input changes*/
$(document).on('upDataData', function( event, key, value){
storedData[key]=value;
$subscribers.filter( '[data-bind="'+key+'"]').text( value );
});
/* change handler for form inputs*/
$(document).on('change', 'form input.data_source', inputChange);
function inputChange(){
$(document).trigger('upDataData',[ this.name, this.value]);
}
/* populate storedData from table elements when page loads*/
function createInitialData(){
$subscribers.each(function(){
storedData[ $(this).data('bind')]= $(this).text();
});
}
createInitialData()
然后在您的fancybox代码中将对象发送到服务器并使用服务器代码将值解析为表单:
$('.fancy').fancybox({
type:'ajax',
/* same as "options" for "$.ajax"*/
ajax: { data : storedData},
afterClose: function(){
alert('done!');
}
});
演示:http: //jsfiddle.net/xg9ML/2/