我需要将选择器的隐藏字段传递给javascript函数。
这是我的功能
function deleteAttachments(id,selector){
$('#proof' + id).remove();
//show warning about save
var tmp = selector.val();
var sep = "";
if (tmp != "")
sep = ",";
selector.val(tmp + sep + id);
}
上述函数调用在以下方法中,
function listAttachments(proofs,selector,hiddenField,after){
//alert(hiddenField.id);
var rows = "<table width=\"70%\">";
for(var i=0; i<proofs.length; i++) {
var proof = proofs[i];
rows += "<tr id=\"proof" + proof["ID"] + "\" width=\"40%\">"
rows += "<td><input type=\"hidden\" value=\"" + proof["filename"] + "\" id=\"Proof" + i + "\" />Uploaded: " + proof["uploaded"] + "</td>"
rows += "<td width=\"90px\"><input type=\"button\" value=\"View...\" onclick=\"viewProof('" + proof["URL"] + "'); \" id=\"btnProof" + i + "\" class=\"btn\"></td>"
rows += "<td width=\"90px\"><input type=\"button\" value=\"Delete\" id=\"btnDelete" + i + "\" onclick=\"deleteAttachments(" + proof["ID"] + "," + hiddenField + ");\" class=\"btn\"/></td></tr>";
}
rows += "</table>";
if(after){
selector.after(rows);
}else{
selector.html(rows);
}
}
请在下面找到listAttachments
函数调用(我正在使用asp.net并尝试不同的方法),
listAttachments(visualIds,$('#tblProofs'),$('#hidDeletedAttachments'),true)
或
listAttachments(visualIds,$('#tblProofs'),$('#' + <%= hidDeletedAttachments.ID%>'),true)
当渲染它时,deleteAttachments
函数接受参数作为对象(如下图所示)。
我的问题是如何将选择器传递给函数并在调用函数中使用它。
答案 0 :(得分:2)
您没有传递选择器,而是传递与选择器匹配的元素集合。
不是将hiddenField传递给listAttachments,而是传递hiddenField id。
listAttachments(visualIds,$('#tblProofs'), 'hidDeletedAttachments'),true)
然后在deleteAttachment函数中创建对象
function deleteAttachments(id,hiddenFieldId){
var selector = $('#' + hiddenFieldId);
$('#proof' + id).remove();
//show warning about save
var tmp = selector.val();
var sep = "";
if (tmp != "")
sep = ",";
selector.val(tmp + sep + id);
}