我正在寻找扩展我最近用jquery编码的脚本。
我有以下代码
<script type='text/javascript'>
added_departments = new Array();
$("#departments_submit").click(function(){
var depo = $("#depo_list").val();
if(jQuery.inArray(depo, added_departments) != -1)
{
return false;
}
else
{
added_departments.push(depo);
$("#depo_added_list").append("<li>" + depo + "<a href='#' title='"+ depo +"' class='remove_depo'> [X] </a></li>");
var current_value = $("#departments").val();
if(current_value)
{
$("#departments").val(current_value + "," + depo);
}
else
{
$("#departments").val(depo);
}
return false;
}
});
</script>
上面的代码采用在选择下拉框中选择的信息,将其添加到div以公开显示,也可以添加到处理数据的隐藏表单字段中。
我试图创建一些能够扭转这种效果并从div和字段中删除某些选择的东西。这是我有这个代码的地方
<script type='text/javascript'>
$(".remove_depo").click(function(){
var removing = $(this).title();
var current_val = $("#deparments").val();
if(current_val == removing) {
$("departments").replace(removing, "");
}
else {
$("departments").replace("," + removing, "");
}
});
</script>
它不会导致任何错误,但它也没有做任何事情?所以我真的被卡住了。有什么想法吗?
编辑:更新代码
$(".remove_depo").click(function(){
var removing = $(this).attr('title');
var current_val = $("#deparments").val();
if(current_val == removing) {
$("#departments").replace(removing, "");
}
else {
$("#departments").replace("," + removing, "");
}
});
这是html
<form method="post" action="javascript:void(0);">Select Departments To Be Added:
<div class="depo_adder">
<select id="depo_list"><option value="">--- INDIVIDUAL TAGS ---</option><option value="blah">blah</option></select>
<button id="departments_submit">Go!</button>
</div></form><form method="post" action="briefings/addbriefing.php">
<div class="form">
<strong>Departments: </strong>
<ul id="depo_added_list"><li>blah<a href="#" title="blah" class="remove_depo"> [X] </a></li></ul>
<input name="departments" id="departments" value="blah" type="hidden">
</div>
答案 0 :(得分:4)
你指的是$('部门') - 这是行不通的。您需要指定一个标识符,例如$('#departmentsments')或类,例如$('。departments)
啊 - 其他答案也是正确的.title()不是函数。你想要
$('#foo')。attr('title')获得标题。