如何将参数传递给jQuery UI对话框事件处理程序? [英] How to pass a parameter to jQuery UI dialog event handler?
问题描述
我目前正在尝试连接jQuery UI对话框,以便我可以使用它来为我的页面创建新项目并修改页面上已有的项目。我在前者管理过。不过,我现在正在努力解决后一个问题。我只是找不到一个很好的方法来传递项目来修改对话框。
I am currently trying to hook up jQuery UI dialog so that I may use it to create new items to my page and to modify ones existing already on the page. I managed in the former. I'm currently struggling in the latter problem, however. I just cannot find a nice way to pass the item to modify to the dialog.
这里有一些代码可以更好地说明问题。请特别注意标有XXX的部分。 {{}}部分源自Django模板语法:
Here's some code to illustrate the issue better. Note especially the part marked with XXX. The {{}} parts are derived from Django template syntax:
$(".exercise").click(function() {
$.post("{{ request.path }}", {
action: "create_dialog",
exercise_name: $(this).text()
},
function(data) {
$("#modify_exercise").html(data.content);
},
"json"
);
$("#modify_exercise").dialog('open');
});
$("#modify_exercise").dialog({
autoOpen: false,
resizable: false,
modal: true,
buttons: {
'{% trans 'Modify' %}': function() {
var $inputs = $('#modify_exercise :input');
var post_values = {};
$inputs.each(function() {
post_values[this.name] = $(this).val();
});
post_values.action = 'validate_form';
//XXX: how to get the exercise name here?
post_values.exercise_name = 'foobar';
$.post('{{ request.path }}', post_values,
function(data) {
if( data.status == 'invalid' ) {
$('#modify_exercise').html(data.content);
}
else {
location.reload();
}
},
"json"
);
}
}
});
这里有一些标记来显示代码与结构的关系:
Here's some markup to show how the code relates to the structure:
<div id="modify_exercise" class="dialog" title="{% trans 'Modify exercise' %}">
</div>
<ul>
{% for exercise in exercises %}
<li>
<a class="exercise" href="#" title="{{ exercise.description }}">
{{ exercise.name }}
</a>
</li>
{% endfor %}
</ul>
推荐答案
如果你正在处理你可能想要的事件处理程序使用事件对象而不是一些全局变量;)
if you're working with eventhandlers you might want to use the event object instead of some global var ;)
event.target就是你要找的东西。
event.target is what you're looking for.
例如
$('.sel').bind('dialogcreate', function(event, ui) {
event.target.innerHTML = 'new content';
});
这篇关于如何将参数传递给jQuery UI对话框事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!