传递带参数的函数

时间:2016-06-04 18:06:15

标签: jquery

我有以下代码:

JQuery的:

alert_question ('Do you want it?', function(dialog){
                    // Do something
                }); 

var alert_question = function(question, yes_reaction_function(dialogItself))
{
    BootstrapDialog.show(
    {
        message: question,
        buttons: [
        {
            label: 'Yes',
            cssClass: 'btn-primary',
            action: yes_reaction_function (dialogItself)        

我知道它看起来不是一个好的代码。我的目标是编写一个通用函数实现对话框,我不知道如何通过成功函数。 截至目前,我在上面代码段的最后一行有一个错误UNEXPECTED TOKEN

1 个答案:

答案 0 :(得分:0)

根据您在下面的代码问题,您可以尝试:

HTML:

<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript" src="jquery-ui-1.7.2.custom.js"></script>
<div id="confirmDialog"></div>
<div id="div_input">
    <input type="button" id="1" value="1" onClick="javascript:confirmAction(updateButton, this.id)" />
</div>

JS:

    function confirmAction(onSuccessFunction, functionArg,alert_question)
    {
        var question = alert_question
        $('#confirmDialog').html(question);
        $('#confirmDialog').dialog({
            modal: true,
            autoOpen: false,
            title: 'Confirm',
            width: 400,
            height: 150,
            buttons: {
                'Yes': function() {
                    $(this).dialog('close');
                    onSuccessFunction(functionArg);
                },
                'No': function() {
                    $(this).dialog('close');
                }
            }
        });
        $("#confirmDialog").dialog('open');
    };
    function updateButton(buttonId)
    {
        alert(buttonId);
        var buttonHTML = '';
        if(buttonId == '1')
        {
            buttonHTML = "<input type='button' id='2' value='2' onClick='javascript:confirmAction(updateButton, this.id)' />";
        }
        else
        {
            buttonHTML = "<input type='button' id='1' value='1' onClick='javascript:confirmAction(updateButton, this.id)' />";
        }
        document.getElementById("div_input").innerHTML = buttonHTML;
    };