我正在尝试根据选择创建唯一的taskID列表。每项任务最多可能有2家公司。我的问题是下面的taskID比较失败。我的代码:
<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>
如何确保列表是唯一的?
提前致谢
答案 0 :(得分:1)
将此视为从最终列表中删除重复项的问题,您可以编写一个类似this SO answer中所示的函数。暂且不说任何concerns with modifying objects you don't own来添加这个功能(事实上它是O(n ^ 2)),该功能允许你这样做:
var theArray = [37,37,36,36,35];
var theUniqueArray = theArray.unique(); // -> [37,36,35]
编辑:我修理了你的小提琴。你有很多问题。看看here。
一些事情:
<script>
标签
$(document).ready(...)
代码。Array.prototype.unique = function()...
是函数表达式,因此不会挂起。我把它移到你的其他代码之上。您从零循环到currTaskIDs
的长度,这是一个字符串,而不是一个数组。因此,当它的值为35,36
时,这是五次迭代,而不是两次。由于unique
函数对数组进行操作,因此我将字符串更改为数组:
if (currTaskIDs.length) arr = currTaskIDs.split(',');
接下来,我们将当前选定的值附加到数组中(如果适用):
if (tid.length) arr.push(tid);
最后我们运行启动整个练习的unique
函数,然后将整个混乱变回一个字符串并将其放入您的字段:
var u = arr.unique();
$("#taskIDList").val(u.join(','));