Javascript 如何将参数传递给jQuery UI对话框事件处理程序?
我目前正在尝试连接jQueryUI对话框,以便使用它在我的页面上创建新项目,并修改页面上已有的项目。我在前一种情况下成功了。然而,我目前正努力解决后一个问题。我就是找不到一个好方法将要修改的项传递到对话框中 这里有一些代码可以更好地说明这个问题。特别注意标有XXX的零件。{{}}部分源自Django模板语法:Javascript 如何将参数传递给jQuery UI对话框事件处理程序?,javascript,jquery,jquery-ui,Javascript,Jquery,Jquery Ui,我目前正在尝试连接jQueryUI对话框,以便使用它在我的页面上创建新项目,并修改页面上已有的项目。我在前一种情况下成功了。然而,我目前正努力解决后一个问题。我就是找不到一个好方法将要修改的项传递到对话框中 这里有一些代码可以更好地说明这个问题。特别注意标有XXX的零件。{{}}部分源自Django模板语法: $(".exercise").click(function() { $.post("{{ request.path }}", { action: "crea
$(".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"
);
}
}
});
下面是一些标记,以显示代码与结构的关系:
<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>
{%用于练习中的练习%}
-
{%endfor%}
我想不出点击事件和对话框之间有什么联系,所以答案可能只是在每次点击事件后使用一个全局变量来存储名称,然后在对话框中使用它
我在这里演示了这个想法:
查看该代码中如何使用
currentItem
。也许以下内容更适合您的口味:
在$(“#修改#u练习”)之前。对话框('open')代码>,添加
$("#modify_exercise").data('exercise_name',$(this).text());
在按钮回调中,替换post_values.exercise_name='foobar'代码>带有
post_values.exercise_name = $(this).data('exercise_name');
如果您正在使用EventHandler,您可能希望使用事件对象而不是某些全局变量;)
event.target是您正在寻找的
e、 g
全局变量现在就可以了。谢谢如果一个对话框有权访问它的“父”元素(调用它的那个),那就更好了。啊,如果它适合他使用它的方式,那么这似乎比我的要好,对你来说。
$('.sel').bind('dialogcreate', function(event, ui) {
event.target.innerHTML = 'new content';
});