Javascript 未使用jQuery replace更改变量

Javascript 未使用jQuery replace更改变量,javascript,jquery,Javascript,Jquery,我试图在单击div时从存储的var中删除文本。我的代码如下所示: $(document).ready(function() { var allids = ''; $("#artistselect").change(function() { var id = $(this).children(":selected").attr("id"); allids.push($(this).children(":selected").attr("value")); selectid

我试图在单击div时从存储的var中删除文本。我的代码如下所示:

$(document).ready(function() {

 var allids = '';

  $("#artistselect").change(function() {

  var id = $(this).children(":selected").attr("id");
  allids.push($(this).children(":selected").attr("value"));

  selectid = "#" + id; 
  echoid = "#artistecho" + id; 

  $(echoid).show(300); 
  $(selectid).hide(0);
  $("input[name=artistslist]").val(allids.join(", "));

  alert(allids);

  });

    $(".remove").click(function() {

    var removeid = $(this).attr("value");

    var allids = allids.replace(removeid, '');

     $('input[name=artistslist]').val(function(index, value) {
       return value.replace(removeid, '');
    });



    alert(allids);
    });
  }); 
此功能工作正常:

$("#artistselect").change(function() {
我对此功能有问题:

$(".remove").click(function() {
它没有修改var allids,也没有给我警报(allids)


提前谢谢

allid必须是数组,并且不应再次使用$(“.remove”)中的var标记。单击(function(){


您应该将allid定义为数组并将selectidechoid定义为变量

   $(document).ready(function() {

      var allids = [];

      $("#artistselect").change(function() {

      var id = $(this).children(":selected").attr("id");
      allids.push($(this).children(":selected").attr("value"));

      var selectid = "#" + id; 
      var echoid = "#artistecho" + id; 

      $(echoid).show(300); 
      $(selectid).hide(0);
      $("input[name=artistslist]").val(allids.join(", "));

      alert(allids);

      });

        $(".remove").click(function() {

        var removeid = $(this).attr("value");

        allids = allids.replace(removeid, '');

         $('input[name=artistslist]').val(function(index, value) {
           return value.replace(removeid, '');
        });



        alert(newids);
        });
      })

您没有在任何位置声明
allid
数组。
allid.replace(removeid,”)
可能是错误的?
allid
是数组吗?请确保使用
>关闭文档就绪功能
。以及
replace()
不是数组的函数,通过在回调中使用
var allids
,您已使其成为局部作用域变量,不再是外部作用域allids。我忘了包括allids实际上已声明且不是数组。即使allids声明为数组,selectid和echoid声明为vars,它也不会改变。然而,它现在给了我警告(allids);
   $(document).ready(function() {

      var allids = [];

      $("#artistselect").change(function() {

      var id = $(this).children(":selected").attr("id");
      allids.push($(this).children(":selected").attr("value"));

      var selectid = "#" + id; 
      var echoid = "#artistecho" + id; 

      $(echoid).show(300); 
      $(selectid).hide(0);
      $("input[name=artistslist]").val(allids.join(", "));

      alert(allids);

      });

        $(".remove").click(function() {

        var removeid = $(this).attr("value");

        allids = allids.replace(removeid, '');

         $('input[name=artistslist]').val(function(index, value) {
           return value.replace(removeid, '');
        });



        alert(newids);
        });
      })