2009-09-28 32 views
0

我期待着扩展最近使用jquery编写的脚本。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> 

回答

4

你指的是$( '部门') - 这是不行的。你需要指定一个标识符,例如$('#departments')或一个类,例如$('。departments)

啊 - 其他答案也是正确的,.title()不是函数。你想

$('#foo')。attr('title')得到标题。

+0

好的,我已经稍微改变了代码...但是它仍然没有按要求工作。 –

相关问题