点击使用jQuery用HTML更新DIV
Updating a DIV with HTML using jQuery on click
我在这里遇到了一个令人烦恼的问题,我需要修复它!
基本上,我有一系列的配料,它们可以拖到搅拌碗上,然后出现在黑板上,配料逐渐消失,URL也会更新。
这一切都很好!
我遇到的问题是,我们为用户提供了另一种向搅拌碗中添加配料的方式,只需单击"添加到碗中"按钮。
你可以在这里看到它的作用。
这个方法会更新"配料"数组,但不会更新黑板、更新URL或淡出架子上的罐子。
要想看到这一点,只需点击架子上的一个罐子,你就会明白我的意思!
因此,在代码中,有问题的片段是:
$(".ingredients_add").click(function () {
// hide the ingredient box
//$("#ingredients_box").toggleClass("hidden");
$("#ingredients_box").toggleClass("visible");
if( ingredients.length <= 4 ){
// get the topping name
var htmlString = $("#ingredients_box > .ingredients_name_label").text();
$('.choc2_flavour').text(htmlString);
//Add item to the array
ingredients.push( htmlString );
// test alert to check array is populating
//alert(ingredients);
// fade out jar --------- we need this to fade out the ingredient jar that has been added. Out of scope??
ui.draggable.fadeOut(500);
//Output the ingredients array -------- this isn't firing and updating the blackboard
drawIngredients();
} else {
//Maybe show an alert here? The ingredient will revert automagically...
alert("You've already added the maximum number of ingredients!");
//ui.draggable({ revert: 'invalid' });
}
});
它似乎不想启动drawIngredients函数。看起来像这样:
//Function to redraw the ingredients list
function drawIngredients(){
//alert("I'm going to draw me a list!");//#DEBUG
//alert(ingredients);//#DEBUG
//Clear down the ingredients div and querystring
$('#ingredient_list').html('');
ingString = "";
//Get the URL
var _href = $("a.to_step_4").attr("href");
//Split the URL
var _hrefs = _href.split("&");
//alert(_href);//#DEBUG
//alert(_hrefs);//#DEBUG
//Add each ingredient into the div
ingredients.forEach( drawIngredient );
//Set the flavour part of the querystring;
_hrefs[2] = "flavour="+ingString;
//alert(_hrefs[2]);//#DEBUG
var _href = _hrefs[0]+"&"+_hrefs[1]+"&"+_hrefs[2];
$("a.to_step_4").attr("href", _href);
}
function drawIngredient( elem, index ){
$('#ingredient_list').append('<p class="choc_ingredient '+index+'">'+elem+'</p>');
ingString += elem+"|";
}
它也不会淡出罐子(可能是因为它超出了范围?)。
有人能帮忙吗?
Simon
变量ui是drop处理程序中的一个参数,因此在调用它时它超出了范围。这会阻止drawIngredients()函数被调用。
如果您需要淡出jar,则需要使用其他机制来获取当前选定的jar,并将其存储以供对话框稍后使用。
也许最好的方法是在罐子上存储数据成分属性,例如:
<li class="drag_me_ingredients ui-draggable"
style="position: relative;" data-ingredient="blueberries">...</li>
然后稍后使用:
$('li[data-ingredient="' + ingredient + '"]').fadeOut(500);
drawIngredients();
这样,您可以查看父<李>当您调用对话框并将其保存以供配料添加例程稍后使用时。
相关文章:
- 添加文字和评论功能更新Div
- 在Rails中更新Div,而不更改更新请求后的视图
- 根据CHECKBOX输入值动态更新DIV信息
- JavaScript on点击更新Div
- 使用不同的API参数更新DIV内容
- 从一个正在运行的PHP代码中,了解如何更新DIV标记中的状态
- 使用2个表单的SQL查询更新DIV
- Rails 在 JavaScript 调用后更新 DIV(从模型重新加载)
- 单击时更新 DIV 内容,而无需直接知道其 ID
- 可观测集合发生变化时更新Div
- 更新DIV的泛型函数
- 点击使用jQuery用HTML更新DIV
- 在jQuery上更新Div隐藏字段可排序拖放更新
- 如何更新DIV的内容
- 更新Div内文本的问题
- 使用来自ASP的数据更新DIV内容.NET上的事件
- 如何更新Div内的文本
- 更新DIV并在1秒后使用setTimeout再次清空它
- 基于顶部位置更新Div的类名
- 更新Div/PartialView上的Interval