0
我想根據選擇創建一個唯一的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>
- 我選擇這兩家公司的任務35. taskIDList文本顯示35 <通>
- 我選擇這兩家公司的任務36,taskIdList文本框顯示:36,36 ,35 <失敗> 它應該顯示36,35。
- 我爲任務37選擇一個公司,taskIdList正確地將37添加到列表中。然後我改變公司,然後taskIdList變成37,37,36,36,35:<失敗>
我如何確保列表是唯一的?
在此先感謝
我添加了$(「#taskIDList」).val(jQuery.unique(arr3));但這不起作用。如果我從任務36中選擇第一個和第二個公司,我的列表將變爲36,36,35 – CFNinja 2010-09-22 21:10:47
「唯一」功能不是內置的。您必須添加如上面鏈接到的答案中所示的功能。這是* not *'jQuery。unique()',它只適用於DOM元素。你嘗試過嗎? – 2010-09-22 21:19:04
添加了Array.prototype.unique並調用arr3 = arr.concat(arr2).unique();但我仍然無法做到。 – CFNinja 2010-09-22 21:31:57