如何获取仅点击按钮和上传文件的json数据(如果有的话),截至目前我在一个案例中获取所有点击/未点击的按钮值并获取上传文件(使用伪路径,如:{{1另一种情况是json格式。我创建了Fiddle,请帮助我以json格式获取点击按钮数据和上传文件(即名称)。
C:\\fakepath\\test.txt

(function() {
var buttonValue = {};
$("#submit").click(function() {
$("input[type=button]" && "input[type=file]").each(function($i) {
var name = $(this).attr('name')
buttonValue[name] = $(this).val();
});
console.log(JSON.stringify(buttonValue)); //I am getting my uploaded file fake path like: C:\\fakepath\\test.txt. not giving the clicked button values in json
});
}())

答案 0 :(得分:1)
您在寻找这样的解决方案:
https://jsfiddle.net/w70zkfod/1/
(function(){
var buttonValue = {};
$("input[type=button]").click(function(){
var name =$(this).attr('name')
buttonValue[name]=$(this).val();
});
$("#submit").click(function(){
$("input[type=file]").each(function($i){
var name =$(this).attr('name')
buttonValue[name]=$(this).val();
});
console.log(JSON.stringify(buttonValue));
});
}())
答案 1 :(得分:0)
选择多个元素的正确方法是在选择器中用逗号分隔它们。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type='button' value='button1' class='button' name='firstButton' />
<br>
<br>
<input type='button' value='button2' class='button' name='secondButton' />
<br>
<br>
<input type='button' value='button3' class='button' name='thirdButton' />
<br>
<br>
<input type='file' name='file' value='file upload' />
<br>
<br>
<input type='submit' value='submit' id='submit'>
&#13;
$(":checkbox:checked")
&#13;
按钮不记得是否被点击过,也许您应该使用复选框。然后,您可以使用{{1}}选择已点击的内容。