我的要求是创建等于json数组计数的按钮数.我成功地在jquery中动态创建按钮.但是没有为点击操作调用jquery的.ready函数中的方法.我试过在SO搜索.找到了一些解决方案,但对我来说没有任 我对jquery很新.请帮忙...
我的代码:jQuery:
$(document).ready(function()
{
currentQuestionNo = 0;
var questionsArray;
$.getJSON('http://localhost/Sample/JsonCreation.php', function(data)
{
questionsArray = data;
variable = 1;
//CREATE QUESTION BUTTONS DYNAMICALLY ** NOT WORKING
for (var question in questionsArray)
{
var button = $("<input>").attr("type", "button").attr("id", "questionButton").val(variable);
$('body').append(button);
//Tried using .next here - but it dint work...
//$('body').append('<button id="questionButton">' + variable + '</button>');
variable++;
}
displayQuestionJS(questionsArray[currentQuestionNo], document);
});
$("button").click(function()
{
if ($(this).attr('id') == "nextQuestion")
{
currentQuestionNo = ++currentQuestionNo;
}
else if ($(this).attr('id') == "previousQuestion")
{
currentQuestionNo = --currentQuestionNo;
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试删除动态附加的元素,但似乎未读取为此元素附加的类函数。
我可以单击+按钮并添加新元素,但是单击“-”按钮时不能删除。
<div id="dftenglist">
<label for="dtfeng">Name:</label><input type="text" class="dfteng">
<button id="plusdfteng">+</button>
</div>
$("#plusdfteng").click(function() {
$("#dftenglist").append('<br><span><label for "a">Name:</label><input type="text" class="dfteng"> <button class="minusbtn">-</button></span>');
});
$(".minusbtn").click(function() {
$(this).parent().remove();
})
Run Code Online (Sandbox Code Playgroud)
http://jsfiddle.net/0uv4k5bz/1/
谢谢亚历克斯