第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

jQuery-Click事件不適用于動態(tài)創(chuàng)建的按鈕

jQuery-Click事件不適用于動態(tài)創(chuàng)建的按鈕

慕慕森 2019-11-29 10:10:18
我的要求是創(chuàng)建等于json數(shù)組計數(shù)的按鈕數(shù)。我成功地在jquery中動態(tài)創(chuàng)建了按鈕。但是單擊動作不會調(diào)用jquery的.ready函數(shù)中的方法。我曾嘗試在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);}
查看完整描述

3 回答

?
三國紛爭

TA貢獻1804條經(jīng)驗 獲得超7個贊

動態(tài)創(chuàng)建按鈕是因為,.live()如果使用jquery 1.7,則需要使用方法調(diào)用它們


但此方法已在新版本中棄用(您可以在此處查看所有棄用方法的列表)。如果要使用jquery 1.10或更高版本,則需要以這種方式調(diào)用按鈕:


$(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

});


查看完整回答
反對 回復 2019-11-29
?
慕后森

TA貢獻1802條經(jīng)驗 獲得超5個贊

我的猜測是,當您綁定按鈕時,您創(chuàng)建的按鈕尚未出現(xiàn)在頁面上。綁定$.getJSON函數(shù)中的每個按鈕,或使用動態(tài)綁定方法,例如:


$('body').on('click', 'button', function() {

    ...

});

注意,您可能不想在'body'標簽上執(zhí)行此操作,而是將按鈕包裝在另一個div或其他內(nèi)容中并對其進行調(diào)用on。


查看完整回答
反對 回復 2019-11-29
?
紅糖糍粑

TA貢獻1815條經(jīng)驗 獲得超6個贊

做到這一點的簡單方法是在事件上使用:


$('body').on('click','#element',function(){

    //somthing

});

但是我們可以說這不是最好的方法。我建議另一種方法是使用clone()方法,而不是使用動態(tài)html。例如,在您的文件中寫一些html:


<div id='div1'></div>

現(xiàn)在,在腳本標簽中對此div進行克隆,然后該div的所有屬性也將帶有新元素。例如:


var dynamicDiv = jQuery('#div1').clone(true);

現(xiàn)在,您可以在想要添加元素或更改其屬性的任何位置使用它?,F(xiàn)在,所有jQuery函數(shù)都可以與此元素一起使用


查看完整回答
反對 回復 2019-11-29
  • 3 回答
  • 0 關注
  • 659 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號