Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

javascript - 如何从选定按钮的输入type = file“ change”中检测数据值?(How to detect data-value from input type=file “change” from the selected button?)

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按钮)

<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');}); }); But here alert($('#upload').data('value')) always showing first time button click value.(但是这里alert($('#upload').data('value'))总是显示第一次按钮单击值。) means when we click on Button 2, alert showing Button 1(表示当我们单击按钮2时,警告显示按钮1)   ask by Note translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

use data('value', 'BUTTON 1') instead of attr('data-value', 'BUTTON 1')(使用data('value', 'BUTTON 1')而不是attr('data-value', 'BUTTON 1'))

same for button 2(按钮2相同)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...