一尘不染

jQuery-Click事件不适用于动态创建的按钮

html

我的要求是创建等于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;
        }

        displayQuestionJS(questionsArray[currentQuestionNo], document);

    });



function displayQuestionJS(currentQuestion, document) 
{
    document.getElementById('questionNumber').innerText  = currentQuestion.questionNumber;
    document.getElementById('questionDescription').innerText  = currentQuestion.quesDesc;
    $('label[for=optionA]').html(currentQuestion.optionA);
    $('label[for=optionB]').html(currentQuestion.optionB);
    $('label[for=optionC]').html(currentQuestion.optionC);
}

HTML content
<form method="post" name="formRadio">

<label id="questionNumber"></label>. &nbsp;
<label id="questionDescription"></label>   <br />
<input type="radio" id="optionA"> </input> <label for="optionA"></label> <br />
<input type="radio" id="optionB"> </input> <label for="optionB"></label> <br />
<input type="radio" id="optionC"> </input> <label for="optionC"></label> <br />

<button id="previousQuestion">Previous Question</button>
<button id="nextQuestion">Next Question</button>

<br />
<br />

<input type="submit" id="submitButton" name="submitTest" value="Submit"></input>
</form>

编辑-示例.on方法代码-单独的文件:工作-谢谢

<script>
$(document).ready(function()
{
    $("button").click(function()
    {
        var button = '<input type="button" id="button2" value="dynamic button">';
        $('body').append(button);
    });
});

$(document).on('click','#button2', function()
{
    alert("Dynamic button action");
});

</script>
</head>

<body>

<button id="button">Static Button</button>

</body>

阅读 246

收藏
2020-05-10

共1个答案

一尘不染

动态创建按钮是因为,.live()如果使用jquery 1.7,则需要使用method 调用它们

但这种方法已被弃用(你可以看到所有的方法已过时的名单这里在更新版本)。如果要使用jquery
1.10或更高版本,则需要以这种方式调用按钮:

$(document).on('click', 'selector', function(){ 
     // Your Code
});

例如

如果您的html是这样的

<div id="btn-list">
    <div class="btn12">MyButton</div>
</div>

你可以这样写你的jQuery

$(document).on('click', '#btn-list .btn12', function(){ 
     // Your Code
});
2020-05-10