数组比较
我正在尝试根据选择创建一个唯一的任务ID 列表。每个任务最多可以有2个公司。我的问题是下面的任务 ID 比较失败。我的代码:
<script>
$().ready(function() {
$.validator.addMethod(
"findRegex",
function(value, element) {
return /^.+::[1-9]\d{0,11}$/.test(value);
},
"Please check your input."
);
$('#myForm').validate({
submitHandler: function(form){
$("#formSub").html('<table style="font-size:11px; color:#333;"><tr><td><img border="0" src="images/ajax-loader.gif"/></td><td>Saving! Please wait...</td></table>');
var options = {
success: showResponse,
url:'addValidation2.cfm?t=1'
};
$('#myForm').ajaxSubmit(options);
return false;
}
});
$('select[name^=compName]').change(function() {
var tid = $(this).attr('name').split('-')[1];
var j = $(this).attr('name').split('-')[2];
var currTaskIDs = $("#taskIDList").val();
// begin: create the task list:
var arr = [];
var arr2 = [];
var arr3 = [];
if (currTaskIDs != '') {
if( $.inArray(currTaskIDs, arr2) == -1) {
arr2.push(currTaskIDs);
}
}
if( $.inArray(tid, arr) == -1) {
arr.push(tid);
}
arr3 = unionArr(arr,arr2);
alert(arr3);
$("#taskIDList").val(arr3);
// end
});
unionArr = function(x, y) {
var obj = {};
for (var i = x.length-1; i >= 0; -- i)
obj[x[i]] = x[i];
for (var i = y.length-1; i >= 0; -- i)
obj[y[i]] = y[i];
var res = []
for (var k in obj) {
if (obj.hasOwnProperty(k))
res.push(obj[k]);
}
return res;
}
$("input[name^=compName-]").each(function() {
$(this).rules("add", { findRegex: true });
});
});
</script>
<form id="myForm" name="myForm" method="post" action="">
task : 35
<select name="compName-35-1" id="compName-35-1">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<select name="compName-35-2" id="compName-35-2" style="padding-left:20px;">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<hr/>
task : 36
<select name="compName-36-1" id="compName-36-1">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<select name="compName-36-2" id="compName-36-2" style="padding-left:20px;">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<hr/>
task : 37
<select name="compName-37-1" id="compName-37-1">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<select name="compName-37-2" id="compName-37-2" style="padding-left:20px;">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<hr/>
<input type="submit" name="btnSave" id="btnSave" value="Save"/>
<hr/>
<input type="text" name="taskIDList" id="taskIDList" value="" />
<div id="formSub"></div>
- 我为任务 35 选择两家公司。taskIDList 文本框显示 35。 <通过>
- 我为任务 36 选择两家公司,taskIdList 文本框显示: 36,36,35 <失败> 它应该显示 36、35。
- 我为任务 37 选择一家公司,taskIdList 正确地将 37 添加到列表中。然后我换了公司,那么taskIdList就变成了37,37,36,36,35:<失败>
我如何确保该列表是唯一的?
提前致谢
I am trying to create a unique taskID list based on the selections. Each task may have 2 companies at the most. My problem is the taskID comparison below fails. My code:
<script>
$().ready(function() {
$.validator.addMethod(
"findRegex",
function(value, element) {
return /^.+::[1-9]\d{0,11}$/.test(value);
},
"Please check your input."
);
$('#myForm').validate({
submitHandler: function(form){
$("#formSub").html('<table style="font-size:11px; color:#333;"><tr><td><img border="0" src="images/ajax-loader.gif"/></td><td>Saving! Please wait...</td></table>');
var options = {
success: showResponse,
url:'addValidation2.cfm?t=1'
};
$('#myForm').ajaxSubmit(options);
return false;
}
});
$('select[name^=compName]').change(function() {
var tid = $(this).attr('name').split('-')[1];
var j = $(this).attr('name').split('-')[2];
var currTaskIDs = $("#taskIDList").val();
// begin: create the task list:
var arr = [];
var arr2 = [];
var arr3 = [];
if (currTaskIDs != '') {
if( $.inArray(currTaskIDs, arr2) == -1) {
arr2.push(currTaskIDs);
}
}
if( $.inArray(tid, arr) == -1) {
arr.push(tid);
}
arr3 = unionArr(arr,arr2);
alert(arr3);
$("#taskIDList").val(arr3);
// end
});
unionArr = function(x, y) {
var obj = {};
for (var i = x.length-1; i >= 0; -- i)
obj[x[i]] = x[i];
for (var i = y.length-1; i >= 0; -- i)
obj[y[i]] = y[i];
var res = []
for (var k in obj) {
if (obj.hasOwnProperty(k))
res.push(obj[k]);
}
return res;
}
$("input[name^=compName-]").each(function() {
$(this).rules("add", { findRegex: true });
});
});
</script>
<form id="myForm" name="myForm" method="post" action="">
task : 35
<select name="compName-35-1" id="compName-35-1">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<select name="compName-35-2" id="compName-35-2" style="padding-left:20px;">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<hr/>
task : 36
<select name="compName-36-1" id="compName-36-1">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<select name="compName-36-2" id="compName-36-2" style="padding-left:20px;">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<hr/>
task : 37
<select name="compName-37-1" id="compName-37-1">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<select name="compName-37-2" id="compName-37-2" style="padding-left:20px;">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<hr/>
<input type="submit" name="btnSave" id="btnSave" value="Save"/>
<hr/>
<input type="text" name="taskIDList" id="taskIDList" value="" />
<div id="formSub"></div>
- i select two companies for task 35. taskIDList textbox displays 35. < pass >
- i select two companies for task 36, taskIdList textbox displays : 36,36,35 < fail >
it should be displaying 36, 35. - i select one company for task 37, taskIdList correctly adds 37 to the list. Then I change the company, then the taskIdList becomes 37,37,36,36,35 : < fail >
how can i make sure that the list is unique ?
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将此视为简单地从最终列表中删除重复项的问题,您可以编写一个小函数,如 这个答案。搁置任何 担心修改您不拥有的对象来添加此功能(事实上它是 O(n^2)),该函数可以让您执行此操作:
编辑:我修复了你的小提琴。你有一堆问题。请查看此处。
有几件事:
标签
$(document).ready(...)
围绕 jsFiddle 中的代码。您正在从零循环到
currTaskIDs
的长度,它是一个字符串,而不是一个数组。因此,当其值为35,36
时,即为五次迭代,而不是两次。由于unique
函数对数组进行操作,因此我已将字符串更改为数组:接下来,我们将当前选定的值附加到数组(如果适用):
最后,我们运行启动整个练习的
unique
函数,然后将整个混乱情况恢复原样放入字符串并将其放入您的字段中:Looking at this as a problem of simply removing duplicates from your final list, you can write a little function like the one shown in this SO answer. Putting aside any concerns with modifying objects you don't own to add this functionality (and the fact that it's O(n^2)), the function lets you do this:
Edit: I fixed up your fiddle. You had a bunch of issues. Take a look here.
A few things:
<script>
tags in jsFiddle$(document).ready(...)
around your code in jsFiddle.Array.prototype.unique = function()...
is a function expression, it is not hoisted. I moved it above your other code.You're looping from zero to the length of
currTaskIDs
, which is a string, not an array. Thus when its value is35,36
, that's five iterations, not two. Since theunique
function operates on arrays, I've changed the string to an array:Next we append the currently selected value to the array, if applicable:
Finally we run the
unique
function that started this whole exercise, then turn the whole mess back into a string and plop it into your field: