2

我想在点击时获得一个按钮 ID 并将其应用于另一个函数。这是我的代码:

<button id="image-upload-button" onClick="reply_click(this.id)" data-link="<?php echo base_url().'admin/phones/upload_image/'; ?>" input-name="image" on-change="imageUpload(this.form);"></button>

和 Javascript:

    //get button id
    function reply_click(clicked_id)
    {
        var button_id = clicked_id;
    }

    function reset() {
        var input_name = $('#' + button_id).attr('input-name');
        var on_change = $('#' + button_id).attr('on-change'); 
        var input = $('<input name="'+ input_name +'" type="file" onchange="'+ on_change +'"/>').appendTo(form);
        input.change(function(e) {
            input.unbind();
            input.detach();
            btn.trigger('choose', [input]);
            reset();
        });
    };

但是,当我添加 $('#' + button_id) 整个脚本停止工作。我该如何解决

4

2 回答 2

3

您已经在 reply_click() 函数的范围内声明了 button_id 。您应该在外部将其声明为全局变量,然后在 reset_click() 函数中对其进行分配。

于 2012-05-02T20:19:54.763 回答
2

button_id必须对 可见reset。在外面声明:

var button_id;

//get button id
function reply_click(clicked_id)
{
    button_id = clicked_id;
}
于 2012-05-02T20:19:10.347 回答