2016-12-30 42 views
0

我試圖從單擊div時刪除存儲的文本中的文本。我的代碼看起來是這樣的:Var未被更改與jQuery替換

$(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);

在此先感謝。

+0

您沒有在任何地方聲明'allids'數組。 – Jai

+0

'allids.replace(removeid,'')'也許錯了? 'allids'是一個數組? – kukkuz

+0

確保你使用'});'來關閉你的文件就緒功能。 – GRA0007

回答

0

allids必須是陣列及其不應該在$ VAR再次decaled( 「刪除」)。點擊(函數(){

$(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"); 

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

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



     alert(newids); 
     }); 
     }); 
0

你應該定義你的allids陣列SELECTID & echoid可變。下面是代碼。

$(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); 
     }); 
     }) 
+0

即使將allids聲明爲數組,並將selectid和echoid聲明爲vars,但它不會更改。但現在卻給了我警戒(allids); – Dustin