When we click on Button 1 or Button 2, trigger the input-file and find trigger is comes from which button using input-file data-value on change event(当我们单击按钮1或按钮2时,触发输入文件并查找触发来自更改事件时使用输入文件data-value按钮)
data-value
<input type="file" id="upload" data-value=""> <button id="button1">button 1</button> <button id="button2">button 2</button> $("document").ready(function(){ $("#upload").change(function() { alert($('#upload').data('value')); }); $('#button1').click(function(e) { $('#upload').attr('data-value', 'BUTTON 1').trigger('click');}); $('#button2').click(function(e) { $('#upload').attr('data-value', 'BUTTON 2').trigger('click');}); });
alert($('#upload').data('value'))
use data('value', 'BUTTON 1') instead of attr('data-value', 'BUTTON 1')(使用data('value', 'BUTTON 1')而不是attr('data-value', 'BUTTON 1'))
data('value', 'BUTTON 1')
attr('data-value', 'BUTTON 1')
1.4m articles
1.4m replys
5 comments
57.0k users