简体   繁体   中英

Kendo UI Mobile - Trouble calling function on button click

I've put together a little app using Kendo UI that stores user inputs in a Javascript array, and then prints these items out by adding text to a div. Along with the text, I need to have a delete button to remove these items from the array.

Since I'm adding the delete buttons to the DOM after I initialize Kendo UI, I assume I need to use the .kendoMobileButton() method on each button I add. If I don't do this, my buttons aren't styled correctly, despite being given the attribute data-role="button" .

Still, when I try to use these buttons, I can't get them to call a function with data-click="deleteNumber" . The function simply doesn't seem to fire. Any hints?

Here is a quick example I threw together that illustrates my problem: http://crocdoc.ifas.ufl.edu/files/kendo_example/

It is pasted here for easy reference:

<!DOCTYPE HTML>
<html>
<head>
<title>Example code</title>
<meta charset="utf-8">
<link rel="stylesheet" href="styles/kendo.mobile.all.min.css" type="text/css">
<script src="js/jquery.min.js"></script>
<script src="js/kendo.mobile.min.js"></script>
</head>
<body>
<div data-role="view" id="main">
    <div data-role="header">
        <div data-role="navbar">Generate numbers</div>
    </div>  

    <a data-role="button" data-click="addNumber" style="display: block; margin: 10px; text-align: center;">Add number</a>
    <div id="number_list" style="padding: 0 20px 0 20px;"></div>

</div>


<script>
    var app = new kendo.mobile.Application();

    var number_container = [];

    var addNumber = function () {
        var current_number = Math.floor(Math.random() * 1000 + 1);
        number_container.push(current_number);
        console.log(number_container);

        var current_index = number_container.length - 1;

        $('#number_list').append('Number ' + current_index + ': ' + current_number + ' <a id="delete_' + current_index + '" data-role="button" data-click="deleteNumber">Delete</a><br >');
        $('#delete_'+current_index).kendoMobileButton();
    };

    var deleteNumber = function (e) {
        console.log('Delete button hit');
        var button_id = e.button.context.id;
        button_id = button_id.replace('delete_', '');
        button_id = parseFloat(button_id);

        number_container.splice(button_id, 1);

        $('#number_list').empty();
        for (var i = 0; i < number_container.length; i++) {
            $('#number_list').append('Number '+i+': '+number_container[i]+' <a id="delete_' + i + '" data-role="button" data-click="deleteNumber">Delete</a><br >');
        }

    }; 

</script>
</body>
</html>

You need to modify your code as following ,

 $('#number_list').append('Number ' + current_index + ': ' + current_number + ' <a id="delete_' + current_index + '">Delete</a><br/>');
    $('#delete_'+current_index).kendoMobileButton({click: deleteNumber});

Thanks DJ

@debug_mode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM