Javascript Jquery:如何在ajax中添加重复数据
我想在ajax中添加重复数据post 但是我不知道哪里有问题 请帮帮我,非常感谢~ 如果输入类为“使用”,请单击“添加活动类” 然后单击finish_sumbit get input employ.active.val()my console.log 但重复就是麻烦~ 这是我的HTMLJavascript Jquery:如何在ajax中添加重复数据,javascript,jquery,ajax,Javascript,Jquery,Ajax,我想在ajax中添加重复数据post 但是我不知道哪里有问题 请帮帮我,非常感谢~ 如果输入类为“使用”,请单击“添加活动类” 然后单击finish_sumbit get input employ.active.val()my console.log 但重复就是麻烦~ 这是我的HTML <input type="button" class="btn ahr-button_2 employ" value="bruce"> <input type="button" class="b
<input type="button" class="btn ahr-button_2 employ" value="bruce">
<input type="button" class="btn ahr-button_2 employ" value="peter">
<input type="button" class="btn ahr-button_2 employ" value="abcd">
<input type="button" class="btn ahr-button_2 employ" value="efgh">
<a href="#" class="finish_sumbit">完了</a>
这是我的Javascript
$('.employ').click(function(){
$(this).toggleClass('active');
});
$(".finish_sumbit").click(function(){
var rows = $('.employ.active');
var employ = $('.employ.active').val();
for(var i = 0; i < rows.length; i++)
{
var data = {'employ':employ[i],'test_id':'1'};
}
console.log(data);
$.ajax({
type: "POST",
url: "business_a",
async:false,
data: {bruce:data,_token:token},
success: function (data) {
alert(JSON.stringify(data));
},
error: function (data) {
alert('error');
}
});
$('.employ')。单击(函数(){
$(this.toggleClass('active');
});
$(“.finish_sumbit”)。单击(函数(){
变量行=$('.employ.active');
var employ=$('.employ.active').val();
对于(变量i=0;i
您的代码中存在多个问题。.val()
将只返回调用集中第一个元素的值,因此employ
将始终是单个值。此外,在循环中,您始终覆盖数据的值
由于可以有多个对象,数据
应该是一个数组
$('.employ')。单击(函数(){
$(this.toggleClass('active');
});
$(“.finish_sumbit”)。单击(函数(e){
e、 预防默认值();
变量数据=$('.employ.active').map(函数(){
返回{
'employ':this.value,
“测试id”:“1”
}
}).get();
控制台日志(数据);
$('pre').text(JSON.stringify(data,null,2));
});
.active{
颜色:红色;
}
在for循环中,每个循环都要初始化新的数据变量!输入var数据;在脚本开始时,for循环中将没有var
使用jQuery扩展。
您需要创建一个数组谢谢~!我得到的雇用数据是使用数据[1][employ]