点击此功能点击

时间:2017-05-19 09:08:30

标签: javascript jquery html onclick midi

我有这个代码: 问题是: 我怎样才能在onClick上触发此代码?

JS:

 $("input[type=radio],button").each(function () {
        var outputName = $(this).attr("data-midioutput");
        if (outputName) {
            var output = MidiUtil.getOutput(outputName);
            if (output) {
                var outputName = $(this).attr("data-midioutput");
                var midiChannel = $(this).attr("data-midichannel");
                if (!midiChannel)
                    midiChannel = 0;

                var cc = $(this).attr("data-midicc");
                var midiValue = $(this).attr("data-midivalue");

                if (outputName && cc) {
                    var output = MidiUtil.getOutput(outputName);
                    if (output) {

                        if ($(this).attr("type") == "number") {
                            this.oninput = function () {
                                    output.send([MidiUtil.eventTypes.MIDI_CC_CH1 + parseInt(midiChannel, 10), parseInt(cc, 10), $(this).val()]);

                            };
                            //Send initial value
                                output.send([MidiUtil.eventTypes.MIDI_CC_CH1 + parseInt(midiChannel, 10), parseInt(cc, 10), $(this).val()]);
                        }
                        else { //button
                            this.onclick = function () {
                                    output.send([MidiUtil.eventTypes.MIDI_CC_CH1 + parseInt(midiChannel, 10), parseInt(cc, 10), parseInt(midiValue, 10)]);
                            };
                        }

                    }
                    else {

                    }
                }
            }

        }
        else {

        }

    });   

这样的按钮会自动触发: HTML:

<button class="1" data-midicc="19" data-midivalue="0" data-midichannel="00" data-midioutput="IAC 1" /> 

问题是:

如何在onClick上触发此代码? 谢谢!

1 个答案:

答案 0 :(得分:0)

只需更改&#39; Jquery-each&#39;单击事件绑定的语句对您有用。

$("input[type=radio],button").on('click',function () {
        var outputName = $(this).attr("data-midioutput");
        if (outputName) {
            var output = MidiUtil.getOutput(outputName);
            if (output) {
                var outputName = $(this).attr("data-midioutput");
                var midiChannel = $(this).attr("data-midichannel");
                if (!midiChannel)
                    midiChannel = 0;

                var cc = $(this).attr("data-midicc");
                var midiValue = $(this).attr("data-midivalue");

                if (outputName && cc) {
                    var output = MidiUtil.getOutput(outputName);
                    if (output) {

                        if ($(this).attr("type") == "number") {
                            this.oninput = function () {
                                    output.send([MidiUtil.eventTypes.MIDI_CC_CH1 + parseInt(midiChannel, 10), parseInt(cc, 10), $(this).val()]);

                            };
                            //Send initial value
                                output.send([MidiUtil.eventTypes.MIDI_CC_CH1 + parseInt(midiChannel, 10), parseInt(cc, 10), $(this).val()]);
                        }
                        else { //button
                            this.onclick = function () {
                                    output.send([MidiUtil.eventTypes.MIDI_CC_CH1 + parseInt(midiChannel, 10), parseInt(cc, 10), parseInt(midiValue, 10)]);
                            };
                        }

                    }
                    else {

                    }
                }
            }

        }
        else {

        }
})