如何:使用 json 中的新内容更新 jQuery 对话框 div
我正在尝试使用 json 数组中的新内容更新对话框的 div,但遇到问题并请求一些指导。
我输出一个 json 数组,其中包含“名称”和“定义”标签。向用户呈现单选按钮列表。当用户单击具有以下结构的单选按钮时:
<input type="radio" value="23" name="statistic" id="stat-23" />
我获取单选按钮的值并使用它来识别我从 json 数组中引用的“名称”“定义”对。
然后,我使用“名称”“定义”对来填充通常会动态更新的 div。为了实现这一点,我使用以下代码:
$('input[type=radio]').live( 'change', function(){
if ( ! $(this).is(':checked') )
return false;
var stat_id = $(this).attr( 'id' ).replace( /stat-/, '' );
refreshDefinition( stat_id );
} );
function refreshDefinition( stat_id ) {
var definition = definitions[ stat_id ];
var div = $("<div id='definition'>"+definition.name+": "+definition.definition+"</div>");
$('#definition').replaceWith( div );
}
这在没有对话框的情况下工作正常(它按原样更新),但是,如果有某种方法合并对话框,以便当用户单击按钮时,它看起来会好得多,将出现对话框,他们可以看到“名称”“定义”对,然后可以在满意后退出。
$('#definition').dialog();
我希望上面的代码显示更新的数据,但它似乎不允许。
如果您对我如何解决这个问题或任何替代方法有任何指导,我将非常感激!
谢谢。
I am attempting to update the div of my dialog with new content from a json array and am encountering issues and am requesting some guidance.
I output a json array which has labels for a 'Name' and a 'Definition'. A user is presented with a list of radio buttons. When a user clicks a radio button which has the following structure:
<input type="radio" value="23" name="statistic" id="stat-23" />
I take the value of the radio button and use this to identify which 'Name' 'Definition' pair I am referring to from my json array.
I then use the 'Name' 'Definition' pair to populate a div which typically updates dynamically. To accomplish this I use the following code:
$('input[type=radio]').live( 'change', function(){
if ( ! $(this).is(':checked') )
return false;
var stat_id = $(this).attr( 'id' ).replace( /stat-/, '' );
refreshDefinition( stat_id );
} );
function refreshDefinition( stat_id ) {
var definition = definitions[ stat_id ];
var div = $("<div id='definition'>"+definition.name+": "+definition.definition+"</div>");
$('#definition').replaceWith( div );
}
This works fine without a dialog (it updates just fine as is), however, it would look a lot better if there were some way to incorporate a dialog so that when a user clicks a button, the dialog will appear and they can see the 'Name' 'Definition' pair and then can exit out of it when they are satisfied.
$('#definition').dialog();
I would like the above code to show the updated data, but it does not appear to allow it.
If you have any guidance on how I could go about solving this problem or any alternative approaches, I would really appreciate it!
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过更新对话框内容?
这是示例。
编辑:
不必要的 .dialog('widget') 使这个答案错误。
Have you tried updating the dialogs content?
Here's an example.
EDIT:
unnecessary .dialog('widget') made this answer wrong.