jQuery附加串联onclick

问题描述 投票:-2回答:2

我正在尝试传递两个变量,一个是整数,另一个是一些字符串,例如我想传递id,name

<div class='redstatus' onclick='redStatus("+Id+","+name+")'><span class='countspan''>"+red_count+"</span></div>

在上述onclick函数中的代码中,如果我仅传递id <div class='redstatus' onclick='redStatus("+Id+")'><span class='countspan''>"+red_count+"</span></div>,则工作正常。

我想再发送一个参数名称以及ID以逗号分隔

<div class='redstatus' onclick='redStatus("+Id+","+name+")'><span class='countspan''>"+red_count+"</span></div>

它不起作用。我需要这方面的帮助。

for(var i in appData ){
    console.log("Data"+JSON.stringify(appData));
    for(j in appData.LOB){

        var LOBId = appData.LOB[j].LOBID;
        LOBName = appData.LOB[j].LOBName;
        var LOBRef = appData.LOB[j].LOBRef;
        var LOBNameRef = appData.LOB[j].LOBNameRef;
        //console.log("LOBId"+LOBId+"LOBName"+LOBName);

        $(".left_div").append("<div class='left_lob_name'>"+LOBName+"</div>");
        streamInRed = [];
        streamInAmber = [];
        streamInGreen = [];
        currentItemRed = [LOBId];
        currentItemAmber = [LOBId];
        currentItemGreen = [LOBId];

        //$("."+LOBNameRef+"").append("<div id="+LOBId+" style='height:74vh;overflow-y:auto;'><table class='table table-bordered' ><thead><tr><th>StreamName</th><th>BusinessSLA Description</th><th>Status</th><th>Business SLA</th><th>Forecast Completion Time</th><th>Actual Completion Time</th><th>JobName</th></tr></thead><tbody class='"+LOBRef+"'></tbody></table></div>");
        for(var k in appData.LOB[j].Streams.Stream){
            //console.log("Streams"+JSON.stringify(appData.LOB[j].Streams.Stream));
            var streamId = appData.LOB[j].Streams.Stream[k].streamId;
            var streamName = appData.LOB[j].Streams.Stream[k].streamName;
            var Status = appData.LOB[j].Streams.Stream[k].Status;
            var jobName = appData.LOB[j].Streams.Stream[k].JobName;
            var BSD= appData.LOB[j].Streams.Stream[k].BusinessSLADescrition;
            var BSLA = appData.LOB[j].Streams.Stream[k].BusinessSLA;
            var FCT = appData.LOB[j].Streams.Stream[k].ForecastCompletionTime;
            var ACT = appData.LOB[j].Streams.Stream[k].ActualCompletionTime;
            var RAGStatus = appData.LOB[j].Streams.Stream[k].RAGStatus;
            if(Status == "red"){
                //$("."+LOBName+"").append("<div class='streamcolor_red test' data-name='1'>"+streamName+"</div>");
                //$("."+LOBRef+"").append("<tr  class='test' data-name='1'><td style='background-color:#f3180d;color:#fff;'>"+streamName+"</td><td>"+BSD+"</td><td>"+RAGStatus+"</td><td>"+BSLA+"</td><td>"+FCT+"</td><td>"+ACT+"</td><td>"+jobName+"</td></tr>");
                red_count = red_count+1;
                currentItemRed = [streamName,BSD,RAGStatus,BSLA,FCT,ACT,jobName];
                streamInRed.push(currentItemRed);
                //redStatus();
                //console.log("streamInRed"+streamInRed);
            }else if(Status == "amber"){
                //$("."+LOBRef+"").append("<tr  class='test' data-name='2'><td style='background-color:rgba(243, 168, 15, 0.9215686274509803);color:#fff;'>"+streamName+"</td><td>"+BSD+"</td><td>"+RAGStatus+"</td><td>"+BSLA+"</td><td>"+FCT+"</td><td>"+ACT+"</td><td>"+jobName+"</td></tr>");
                //$("."+LOBName+"").append("<div class='streamcolor_amber test' data-name='2'>"+streamName+"</div>");
                amber_count = amber_count+1;
                currentItemAmber = [streamName,BSD,RAGStatus,BSLA,FCT,ACT,jobName];
                streamInAmber.push(currentItemAmber);
            }else {
                //$("."+LOBRef+"").append("<tr  class='test' data-name='3'><td style='background-color:green;color:#fff;'>"+streamName+"</td><td>"+BSD+"</td><td>"+RAGStatus+"</td><td>"+BSLA+"</td><td>"+FCT+"</td><td>"+ACT+"</td><td>"+jobName+"</td></tr>");
                //$("."+LOBName+"").append("<div class='streamcolor_green test' data-name='3'>"+streamName+"</div>");
                green_count = green_count+1;
                currentItemGreen=[streamName,BSD,RAGStatus,BSLA,FCT,ACT,jobName];
                streamInGreen.push(currentItemGreen);
            }
            //console.log("streamId"+streamId+"streamName"+streamName+"Status"+Status);
        }
        console.log("LOBId",LOBId);
        console.log("sep_symbol",sep_symbol);
        console.log("syb",syb);
        console.log("LOBNameRef",LOBNameRef);
        var tempvar = "'"+LOBNameRef+"'";
        console.log("tempvar"+LOBId +sep_symbol +tempvar);
        $("<div style='text-align:center;height:5vh;margin:2vw;'> <div class='redstatus' onclick='redStatus("+LOBId+","+LOBNameRef+")'><span class='countspan''>"+red_count+"</span></div> <div class='amberstatus' onclick='amberStatus("+LOBId+")'><span class='countspan'>"+amber_count+"</span></div> <div class='greenstatus' onclick='greenStatus("+LOBId+")'><span class='countspan'>"+green_count+"</span></div></div>").appendTo(".right_div");
        red_count = 0;
        amber_count = 0;
        green_count = 0;
        //var Streams = appData.LOB[j].Streams;
        //console.log("Before"+$wrapper);
        //var $wrapper = $('.'+LOBRef+'');
        //console.log("after"+$wrapper);
        //$wrapper.find('.test').sort(function (a, b) {
        /// return +a.dataset.name - +b.dataset.name;
        //})
        //.appendTo( $wrapper );

            }
}

我已添加代码供您参考

javascript jquery append
2个回答
-1
投票

您想连接传递给函数的两个参数,不要将变量括在单引号或双引号中,它应为:onclick =“ redStatus(Id,name)”


-1
投票

您可以查看以下示例,是否有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.