在jquery中替换div中的文本时遇到问题

mrfwxfqh  于 2023-08-04  发布在  jQuery
关注(0)|答案(1)|浏览(102)

这应该很简单。我做过很多次了。但由于某种原因,我在写div中的文本时遇到了麻烦。
div在html中是硬编码的,但我在函数内部通过on("click")调用引用div。也许这是一个范围的问题。

var objPlayerdata; //object array of arrays of players straight from database
var objUnpicked = {}; //array of array of players available to be picked
getPlayers();

function getPlayers() { //initial download of player data

    objUnpicked = [{"PlayerID":"1","Num":"57","First":"Montravius","Last":"Adams","Suffix":"","Position":"DL","Photo":"adams-montravius","Height":"6-4","Weight":"304","College":"Auburn","Experience":"7","Summary":"Nose tackle played 26% of the defensive snaps."},{"PlayerID":"71","Num":"74","First":"Spencer","Last":"Anderson","Suffix":"","Position":"OL","Photo":"anderson-spencer","Height":"6-5","Weight":"305","College":"Maryland","Experience":"R","Summary":"Second 7th-rounder has flexibility to play G or T."},{"PlayerID":"20","Num":"19","First":"Calvin","Last":"Austin","Suffix":"III","Position":"WR","Photo":"austin-calvin-iii","Height":"5-9","Weight":"162","College":"Memphis","Experience":"2","Summary":"Has speed to be running\/receiving\/return threat."},{"PlayerID":"38","Num":"35","First":"Luq","Last":"Barcoo","Suffix":"","Position":"CB","Photo":"barcoo-luq","Height":"6-1","Weight":"180","College":"San Diego State","Experience":"2","Summary":"Lanky XFL All-Star but a long shot to make it."},{"PlayerID":"88","Num":"95","First":"Keeanu","Last":"Benton","Suffix":"","Position":"DL","Photo":"benton-keeanu","Height":"6-4","Weight":"309","College":"Wisconsin","Experience":"R","Summary":"Could play right away, or watch and learn for now."},{"PlayerID":"12","Num":"9","First":"Chris","Last":"Boswell","Suffix":"","Position":"Special","Photo":"boswell-chris","Height":"6-2","Weight":"185","College":"Rice","Experience":"9","Summary":"Had a down year while dealing with groin injury."},{"PlayerID":"15","Num":"13","First":"Miles","Last":"Boykin","Suffix":"","Position":"WR","Photo":"boykin-miles","Height":"6-4","Weight":"220","College":"Notre Dame","Experience":"5","Summary":"Special teams coverage could keep him around."},{"PlayerID":"79","Num":"84","First":"Ja'Marcus","Last":"Bradley","Suffix":"","Position":"WR","Photo":"bradley-jaMarcus","Height":"6-0","Weight":"198","College":"La.-Lafayette","Experience":"2","Summary":"Former Brown J.B. will try to look like A.B. in camp."},{"PlayerID":"22","Num":"21","First":"Hakeem","Last":"Butler","Suffix":"","Position":"WR","Photo":"butler-hakeem","Height":"6-5","Weight":"227","College":"Iowa State","Experience":"2","Summary":"Huge wideout rekindled his NFL chances in XFL."},{"PlayerID":"18","Num":"16","First":"Jordan","Last":"Byrd","Suffix":"","Position":"WR","Photo":"byrd-jordan","Height":"5-9","Weight":"170","College":"San Diego State","Experience":"R","Summary":"KR\/PR hopes to make some Byrd calls this summer."},{"PlayerID":"80","Num":"85","First":"Dan","Last":"Chisena","Suffix":"","Position":"WR","Photo":"chisena-dan","Height":"6-3","Weight":"203","College":"Penn State","Experience":"3","Summary":"Unless he's a gunner, he's not making the team."},{"PlayerID":"64","Num":"67","First":"Le'Raven","Last":"Clark","Suffix":"","Position":"OL","Photo":"clark-leRaven","Height":"6-5","Weight":"311","College":"Texas Tech","Experience":"8","Summary":"Journeyman is insurance policy for the 20-somethings."},{"PlayerID":"59","Num":"61","First":"Mason","Last":"Cole","Suffix":"","Position":"OL","Photo":"cole-mason","Height":"6-5","Weight":"298","College":"Michigan","Experience":"6","Summary":"Played hurt but didn't miss a game as starting C."},{"PlayerID":"58","Num":"60","First":"Dylan","Last":"Cook","Suffix":"","Position":"OL","Photo":"cook-dylan","Height":"6-6","Weight":"305","College":"Montana","Experience":"1","Summary":"Project who converted from QB in college (yes, really)."},{"PlayerID":"74","Num":"78","First":"James","Last":"Daniels","Suffix":"","Position":"OL","Photo":"daniels-james","Height":"6-4","Weight":"327","College":"Iowa","Experience":"6","Summary":"Young veteran didn't miss a snap at RG in first year here."},{"PlayerID":"36","Num":"33","First":"Duke","Last":"Dawson","Suffix":"","Position":"DB","Photo":"dawson-duke","Height":"5-10","Weight":"198","College":"Florida","Experience":"4","Summary":"On practice squad in '22; competing at slot corner."},{"PlayerID":"67","Num":"69","First":"Kevin","Last":"Dotson","Suffix":"","Position":"OL","Photo":"dotson-kevin","Height":"6-4","Weight":"321","College":"La.-Lafayette","Experience":"3","Summary":"Committed 12 penalties in first full season; now a backup."},{"PlayerID":"65","Num":"68","First":"William ","Last":"Dunkle","Suffix":"","Position":"OL","Photo":"dunkle-william","Height":"6-5","Weight":"330","College":"San Diego State","Experience":"1","Summary":"Heavy hands but lack of athleticism is his weakness."},{"PlayerID":"89","Num":"96","First":"Breiden","Last":"Fehoko","Suffix":"","Position":"DL","Photo":"fehoko-breiden","Height":"6-3","Weight":"300","College":"LSU","Experience":"3","Summary":"On his to-do list: Stop the run, stop the run, stop the run."},{"PlayerID":"42","Num":"39","First":"Minkah","Last":"Fitzpatrick","Suffix":"","Position":"S","Photo":"fitzpatrick-minkah","Height":"6-1","Weight":"207","College":"Alabama","Experience":"6","Summary":"Reigning team MVP is among the league's best."},{"PlayerID":"77","Num":"82","First":"Dez","Last":"Fitzpatrick","Suffix":"","Position":"WR","Photo":"fitzpatrick-dez","Height":"6-2","Weight":"208","College":"Louisville","Experience":"2","Summary":"Sleeper pick to have a productive camp\/preseason."},{"PlayerID":"82","Num":"88","First":"Pat","Last":"Freiermuth","Suffix":"","Position":"TE","Photo":"freiermuth-pat","Height":"6-5","Weight":"258","College":"Penn State","Experience":"3","Summary":"Here's a play call for Matt Canada: 3rd-and-Muth."},{"PlayerID":"76","Num":"81","First":"Zach","Last":"Gentry","Suffix":"","Position":"TE","Photo":"gentry-zach","Height":"6-8","Weight":"265","College":"Michigan","Experience":"5","Summary":"Can be a bridge to Washington on a one-year deal."},{"PlayerID":"55","Num":"54","First":"Markus","Last":"Golden","Suffix":"","Position":"LB","Photo":"golden-markus","Height":"6-2","Weight":"260","College":"Missouri","Experience":"9","Summary":"Brings 47 career sacks to No. 3 pass rush role."},{"PlayerID":"31","Num":"29","First":"Alfonzo","Last":"Graham","Suffix":"","Position":"RB","Photo":"graham-alfonzo","Height":"5-9","Weight":"180","College":"Morgan State","Experience":"R","Summary":"Undersized UDFA signed after rookie camp tryout."},{"PlayerID":"54","Num":"53","First":"Kendrick","Last":"Green","Suffix":"","Position":"OL","Photo":"green-kendrick","Height":"6-4","Weight":"315","College":"Illinois","Experience":"3","Summary":"Working at C again but hanging by a thread."},{"PlayerID":"27","Num":"25","First":"Darius","Last":"Hagans","Suffix":"","Position":"RB","Photo":"hagans-darius","Height":"6-0","Weight":"210","College":"Virginia State","Experience":"R","Summary":"Averaged 5.4 yards per carry in '22 in Division II."},{"PlayerID":"40","Num":"38","First":"Madre","Last":"Harper","Suffix":"","Position":"CB","Photo":"harper-madre","Height":"6-1","Weight":"196","College":"Southern Illinois","Experience":"2","Summary":"Brings NFL experience to bottom of the roster."},{"PlayerID":"24","Num":"22","First":"Najee","Last":"Harris","Suffix":"","Position":"RB","Photo":"harris-najee","Height":"6-2","Weight":"230","College":"Alabama","Experience":"3","Summary":"Durable in back-to-back 1,000-yard seasons."},{"PlayerID":"10","Num":"6","First":"Pressley","Last":"Harvin","Suffix":"III","Position":"Special","Photo":"harvin-pressley-iii","Height":"6-0","Weight":"255","College":"Georgia Tech","Experience":"3","Summary":"Must hold off NFL-caliber competition in Year 3."},{"PlayerID":"53","Num":"51","First":"Nick","Last":"Herbig","Suffix":"","Position":"LB","Photo":"herbig-nick","Height":"6-2","Weight":"240","College":"Wisconsin","Experience":"R","Summary":"Posted 20 sacks, 30 TFLs as sophomore and junior."},{"PlayerID":"68","Num":"71","First":"Nate","Last":"Herbig","Suffix":"","Position":"OL","Photo":"herbig-nate","Height":"6-4","Weight":"334","College":"Stanford","Experience":"5","Summary":"Can he also be backup C? (49 career snaps there.)"},{"PlayerID":"78","Num":"83","First":"Connor","Last":"Heyward","Suffix":"","Position":"TE","Photo":"heyward-connor","Height":"6-0","Weight":"230","College":"Michigan State","Experience":"2","Summary":"Offensive chess piece has chemistry with No. 8."},{"PlayerID":"90","Num":"97","First":"Cam","Last":"Heyward","Suffix":"","Position":"DL","Photo":"heyward-cameron","Height":"6-5","Weight":"295","College":"Ohio State","Experience":"13","Summary":"Just 2.5 sacks away from all-time franchise record."},{"PlayerID":"57","Num":"56","First":"Alex","Last":"Highsmith","Suffix":"","Position":"LB","Photo":"highsmith-alex","Height":"6-4","Weight":"242","College":"Charlotte","Experience":"4","Summary":"Breakout campaign led to lucrative new contract."},{"PlayerID":"56","Num":"55","First":"Cole","Last":"Holcomb","Suffix":"","Position":"LB","Photo":"holcomb-cole","Height":"6-1","Weight":"240","College":"North Carolina","Experience":"5","Summary":"Signed to be every-down presence in middle of field."},{"PlayerID":"41","Num":"38","First":"Jason","Last":"Huntley","Suffix":"","Position":"RB","Photo":"huntley-jason","Height":"5-9","Weight":"195","College":"New Mexico State","Experience":"2","Summary":"Practice squad holdover is small but quick."},{"PlayerID":"19","Num":"18","First":"Diontae","Last":"Johnson","Suffix":"","Position":"WR","Photo":"johnson-diontae","Height":"5-10","Weight":"183","College":"Toledo","Experience":"5","Summary":"Had 16 1st-down catches on 3rd-down, but no TDs."},{"PlayerID":"69","Num":"72","First":"Manny","Last":"Jones","Suffix":"","Position":"DL","Photo":"jones-manny","Height":"6-4","Weight":"290","College":"Colorado State","Experience":"1","Summary":"One of many D-linemen, and way down the line."},{"PlayerID":"73","Num":"77","First":"Broderick","Last":"Jones","Suffix":"","Position":"OL","Photo":"jones-broderick","Height":"6-5","Weight":"311","College":"Georgia","Experience":"R","Summary":"Left tackle of the future; maybe the present, too."},{"PlayerID":"25","Num":"23","First":"Damontae","Last":"Kazee","Suffix":"","Position":"S","Photo":"kazee-damontae","Height":"5-11","Weight":"174","College":"San Diego State","Experience":"7","Summary":"Couple INTs in 9 games helped get him re-signed."},{"PlayerID":"30","Num":"28","First":"Miles","Last":"Killebrew","Suffix":"","Position":"S","Photo":"killebrew-miles","Height":"6-2","Weight":"222","College":"Southern Utah","Experience":"8","Summary":"Back again as cerebral special teams captain."},{"PlayerID":"49","Num":"46","First":"Christian","Last":"Kuntz","Suffix":"","Position":"Special","Photo":"kuntz-christian","Height":"6-1","Weight":"227","College":"Duquesne","Experience":"3","Summary":"Local guy living the dream as a reliable snapper."},{"PlayerID":"2","Num":"--","First":"Nick","Last":"Kwiatkoski","Suffix":"","Position":"LB","Photo":"kwiatkoski-nick","Height":"6-2","Weight":"242","College":"West Virginia","Experience":"8","Summary":"Experienced Bethel Park grad signed after minicamp tryout."},{"PlayerID":"91","Num":"98","First":"DeMarvin","Last":"Leal","Suffix":"","Position":"DL","Photo":"leal-deMarvin","Height":"6-4","Weight":"290","College":"Texas A&M","Experience":"2","Summary":"Still looking for first NFL sack after missing 6 games."},{"PlayerID":"86","Num":"92","First":"Isaiahh","Last":"Loudermilk","Suffix":"","Position":"DL","Photo":"loudermilk-isaiahh","Height":"6-7","Weight":"293","College":"Wisconsin","Experience":"3","Summary":"Started 3 games, then barely played after Week 9."},{"PlayerID":"8","Num":"4","First":"Braden","Last":"Mann","Suffix":"","Position":"Special","Photo":"mann-braden","Height":"5-11 ","Weight":"198","College":"Texas A&M","Experience":"4","Summary":"Jets punter the last 3 years; won Ray Guy Award in '18."},{"PlayerID":"85","Num":"91","First":"Jonathan","Last":"Marshall","Suffix":"","Position":"DL","Photo":"marshall-jonathan","Height":"6-3","Weight":"310","College":"Arkansas","Experience":"2","Summary":"Thickest DL on roster but it's a deep unit."},{"PlayerID":"60","Num":"62","First":"Ryan","Last":"McCollum","Suffix":"","Position":"OL","Photo":"mccollum-ryan","Height":"6-5","Weight":"300","College":"Texas A&M","Experience":"2","Summary":"Only path to roster is to win backup C job."},{"PlayerID":"28","Num":"26","First":"Anthony","Last":"McFarland","Suffix":"Jr.","Position":"RB","Photo":"mcFarland-anthony-jr","Height":"5-8","Weight":"193","College":"Maryland","Experience":"3","Summary":"Vying to be RB3 now that Benny Snell is gone."},{"PlayerID":"62","Num":"65","First":"Dan","Last":"Moore","Suffix":"Jr.","Position":"OL","Photo":"moore-dan-jr","Height":"6-5","Weight":"315","College":"Texas A&M","Experience":"3","Summary":"Has started 34 of possible 35 games so far at LT."},{"PlayerID":"9","Num":"5","First":"Tanner","Last":"Morgan","Suffix":"","Position":"QB","Photo":"morgan-tanner","Height":"6-2","Weight":"215 ","College":"Minnesota","Experience":"R","Summary":"Up-and-down 5-year starter in Big Ten went undrafted."},{"PlayerID":"47","Num":"44","First":"Tanner","Last":"Muse","Suffix":"","Position":"LB","Photo":"muse-tanner","Height":"6-2","Weight":"227","College":"Clemson","Experience":"4","Summary":"Mainly here for special teams like the old No. 44."},{"PlayerID":"48","Num":"45","First":"Toby","Last":"Ndukwe","Suffix":"","Position":"LB","Photo":"ndukwe-toby","Height":"6-3","Weight":"250","College":"Sam Houston State","Experience":"R","Summary":"Odds are slightly higher than Sam Houston himself."},{"PlayerID":"35","Num":"31","First":"Keanu","Last":"Neal","Suffix":"","Position":"S","Photo":"neal-keanu","Height":"6-0","Weight":"211","College":"Florida","Experience":"8","Summary":"Strong safety allowed 3 TDs last year with Bucs."},{"PlayerID":"23","Num":"21","First":"Tre","Last":"Norwood","Suffix":"","Position":"S","Photo":"norwood-tre","Height":"6-0","Weight":"194","College":"Oklahoma","Experience":"3","Summary":"Firmly established as Minkah Fitzpatrick's backup."},{"PlayerID":"63","Num":"66","First":"James","Last":"Nyamwaya","Suffix":"","Position":"DL","Photo":"nyamwaya-james","Height":"6-5","Weight":"305","College":"Merrimack","Experience":"R","Summary":"UDFA came long way from Division III ranks, but ... "},{"PlayerID":"92","Num":"99","First":"Larry","Last":"Ogunjobi","Suffix":"","Position":"DL","Photo":"ogunjobi-larry","Height":"6-3","Weight":"305","College":"Charlotte","Experience":"7","Summary":"Got 3 years, $28.75 million but it's back-loaded."},{"PlayerID":"72","Num":"76","First":"Chukwuma","Last":"Okorafor","Suffix":"","Position":"OL","Photo":"okorafor-chukwuma","Height":"6-6","Weight":"320","College":"W. Michigan","Experience":"6","Summary":"PFF grade (61.2) was lowest among team's OL."},{"PlayerID":"83","Num":"89","First":"Gunner","Last":"Olszewski","Suffix":"","Position":"WR","Photo":"olszewski-gunner","Height":"6-0","Weight":"190","College":"Bemidji State","Experience":"5","Summary":"Just 6.9 yards\/punt return before getting benched."},{"PlayerID":"43","Num":"40","First":"David","Last":"Perales","Suffix":"","Position":"LB","Photo":"perales-david","Height":"6-3","Weight":"255","College":"Fresno State","Experience":"R","Summary":"UDFA rushes hard but has an uphill climb."},{"PlayerID":"21","Num":"20","First":"Patrick","Last":"Peterson","Suffix":"","Position":"CB","Photo":"peterson-patrick","Height":"6-1","Weight":"203","College":"LSU","Experience":"13","Summary":"Just turned 33 -- and coming off 5 INTs for Vikings."},{"PlayerID":"16","Num":"14","First":"George","Last":"Pickens","Suffix":"","Position":"WR","Photo":"pickens-george","Height":"6-3","Weight":"200","College":"Georgia","Experience":"2","Summary":"Only 52 catches as a rookie but 25 of 20+ yards"},{"PlayerID":"11","Num":"8","First":"Kenny","Last":"Pickett","Suffix":"","Position":"QB","Photo":"pickett-kenny","Height":"6-3","Weight":"220","College":"Pitt","Experience":"2","Summary":"Tossed 5 TDs to just 1 INT over final 8 games."},{"PlayerID":"46","Num":"42 ","First":"James","Last":"Pierre","Suffix":"","Position":"CB","Photo":"pierre-james","Height":"6-2","Weight":"185","College":"Fla. Atlantic","Experience":"4","Summary":"Job in jeopardy as young studs join his group."},{"PlayerID":"26","Num":"24","First":"Joey","Last":"Porter","Suffix":"Jr.","Position":"CB","Photo":"porter-joey-jr","Height":"6-2","Weight":"193","College":"Penn State","Experience":"R","Summary":"No INTs but 11 pass breakups last season."},{"PlayerID":"44","Num":"40","First":"Monte","Last":"Pottebaum","Suffix":"","Position":"RB","Photo":"pottebaum-monte","Height":"6-1","Weight":"244","College":"Iowa","Experience":"R","Summary":"Mullet + wants to hit people = folk hero fullback."},{"PlayerID":"5","Num":"3","First":"B.T.","Last":"Potter","Suffix":"","Position":"Special","Photo":"potter-bt","Height":"5-10 ","Weight":"200","College":"Clemson","Experience":"R","Summary":"Made 75.3% of FGs and all but one XP in college."},{"PlayerID":"39","Num":"37","First":"Elijah","Last":"Riley","Suffix":"","Position":"S","Photo":"riley-elijah","Height":"6-0","Weight":"205","College":"Army","Experience":"3","Summary":"Versatile backup safety and inside corner."},{"PlayerID":"52","Num":"50","First":"Elandon","Last":"Roberts","Suffix":"","Position":"LB","Photo":"roberts-elandon","Height":"6-1","Weight":"238","College":"Houston","Experience":"8","Summary":"Career-best 107 tackles for Dolphins in '22."},{"PlayerID":"14","Num":"11","First":"Allen","Last":"Robinson","Suffix":"II","Position":"WR","Photo":"robinson-allen-ii","Height":"6-2","Weight":"220","College":"Penn State","Experience":"10","Summary":"Caught 33 passes for 339 yards and 3 TDs in '22."},{"PlayerID":"45","Num":"41","First":"Kenny","Last":"Robinson","Suffix":"","Position":"S","Photo":"robinson-kenny","Height":"6-2","Weight":"200","College":"West Virginia","Experience":"3","Summary":"Wilkinsburg native aiming to revive his career at home."},{"PlayerID":"87","Num":"93","First":"Mark","Last":"Robinson","Suffix":"","Position":"LB","Photo":"robinson-mark","Height":"5-11","Weight":"235","College":"Mississippi","Experience":"2","Summary":"Explosive athlete but needs to learn the position."},{"PlayerID":"50","Num":"48","First":"Quincy","Last":"Roche","Suffix":"","Position":"LB","Photo":"roche-quincy","Height":"6-3","Weight":"245","College":"Miami","Experience":"2","Summary":"6th-rounder in '21 circles back for Round 2."},{"PlayerID":"4","Num":"2","First":"Mason","Last":"Rudolph","Suffix":"","Position":"QB","Photo":"rudolph-mason","Height":"6-5 ","Weight":"235","College":"Oklahoma State","Experience":"6","Summary":"Didn't see a snap last regular season as No. 3 QB."},{"PlayerID":"51","Num":"49","First":"Chapelle","Last":"Russell","Suffix":"","Position":"LB","Photo":"russell-chapelle","Height":"6-1","Weight":"230","College":"Temple","Experience":"3","Summary":"Added to practice squad last year after roster cuts."},{"PlayerID":"70","Num":"73","First":"Isaac","Last":"Seumalo","Suffix":"","Position":"OL","Photo":"seumalo-isaac","Height":"6-4","Weight":"303","College":"Oregon State","Experience":"8","Summary":"Technician who brings Eagles experience to LG job."},{"PlayerID":"37","Num":"34","First":"Chandon","Last":"Sullivan","Suffix":"","Position":"CB","Photo":"sullivan-chandon","Height":"5-11","Weight":"189","College":"Georgia State","Experience":"6","Summary":"Nickel had career-high 60 stops last year for Vikings."},{"PlayerID":"3","Num":"--","First":"Rex","Last":"Sunahara","Suffix":"","Position":"Special","Photo":"sunahara-rex","Height":"6-6 ","Weight":"242 ","College":"West Virginia","Experience":"1","Summary":"Will try to unseat incumbent snapper Christian Kuntz."},{"PlayerID":"29","Num":"27","First":"Cory","Last":"Trice","Suffix":"Jr.","Position":"CB","Photo":"trice-cory-jr","Height":"6-3","Weight":"206","College":"Purdue","Experience":"R","Summary":"7th-rounder with size eager to add CB depth."},{"PlayerID":"13","Num":"10","First":"Mitch","Last":"Trubisky","Suffix":"","Position":"QB","Photo":"trubisky-mitch","Height":"6-2","Weight":"220","College":"North Carolina","Experience":"7","Summary":"Signed new deal to be Pickett's long-term backup."},{"PlayerID":"32","Num":"29","First":"Levi","Last":"Wallace","Suffix":"","Position":"CB","Photo":"wallace-levi","Height":"6-0","Weight":"179","College":"Alabama","Experience":"6","Summary":"Opportunistic, settled in as starter down stretch."},{"PlayerID":"33","Num":"30","First":"Jaylen","Last":"Warren","Suffix":"","Position":"RB","Photo":"warren-jaylen","Height":"5-8","Weight":"215","College":"Oklahoma State","Experience":"2","Summary":"Proved worth as capable rusher, receiver, blocker."},{"PlayerID":"75","Num":"80","First":"Darnell","Last":"Washington","Suffix":"","Position":"TE","Photo":"washington-darnell","Height":"6-7","Weight":"264","College":"Georgia","Experience":"R","Summary":"Loves to block and an intriguing red-zone target."},{"PlayerID":"84","Num":"90","First":"T.J.","Last":"Watt","Suffix":"","Position":"LB","Photo":"watt-tj","Height":"6-4","Weight":"252","College":"Wisconsin","Experience":"7","Summary":"Elite when healthy, so he's due for a bounce-back."},{"PlayerID":"66","Num":"68","First":"Armon","Last":"Watts","Suffix":"","Position":"DL","Photo":"watts-armon","Height":"6-5","Weight":"307","College":"Arkansas","Experience":"5","Summary":"Started Weeks 4-16 in only season with Bears."},{"PlayerID":"17","Num":"15","First":"Cody","Last":"White","Suffix":"","Position":"WR","Photo":"white-cody","Height":"6-3","Weight":"215","College":"Michigan State","Experience":"2","Summary":"Has been on practice squad or roster since '20."},{"PlayerID":"34","Num":"30","First":"Chris","Last":"Wilcox","Suffix":"","Position":"CB","Photo":"wilcox-chris","Height":"6-2","Weight":"195","College":"BYU","Experience":"1","Summary":"Depth piece was 7th-round pick of Bucs in '21."},{"PlayerID":"61","Num":"64","First":"Jarrid","Last":"Williams","Suffix":"","Position":"OL","Photo":"williams-jarrid","Height":"6-6","Weight":"308","College":"Miami","Experience":"1","Summary":"Camp body at tackle who was undrafted in '22."},{"PlayerID":"81","Num":"87","First":"Rodney","Last":"Williams","Suffix":"","Position":"TE","Photo":"williams-rodney","Height":"6-4","Weight":"235","College":"Tennessee-Martin","Experience":"1","Summary":"Athletic ex-WR but has been glued to practice squad."}];
    getPosition("ALL");

}

function getPosition(thisPosition) {

    //reflect instructions for each position
      $('.instructions').empty();
      switch(thisPosition) {
        case 'QB':
        $('.instructions').html('Pick a maximum of 3 QBs.');
        break;
        case 'RB':
        $('.instructions').html('Pick a maximum of 5 RBs.');
        break;
        case 'WR':
        $('.instructions').html('Pick a maximum of 7 WRs.');
        break;
         case 'TE':
        $('.instructions').html('Pick a maximum of 4 TEs.');
        break;
        case 'OL':
        $('.instructions').html('Pick a maximum of 9 OLs.');
        break;
         case 'DL':
        $('.instructions').html('Pick a maximum of 9 DLs.');
        break;
         case 'LB':
        $('.instructions').html('Pick a maximum of 8 LBs.');
        break;
        case 'CB':
        $('.instructions').html('Pick a maximum of 7 CBs.');
        break;
        case 'S':
        $('.instructions').html('Pick a maximum of 5 Ss.');
        break;
        case 'Special':
        $('.instructions').html('Pick a maximum of 3 special team members.');
        break;
        case 'ALL':
        $('.instructions').html('');
        break;
        default:
        // code block
      }

     $('.players').empty(); //get rid of any former players showing on the web page
    var appendString = ""; //initialize string of html of player data

    //look through unpicked players object and return those that match clicked position
    for (var i = 0; i< objUnpicked.length; i++) { //go through the object of unpicked players
      if (thisPosition == "ALL") { //if they want to display all players, display them all
        appendString += "<div class='player'>\n"
               + "<div class='headshot'><img src='img/" + objUnpicked[i].Photo + ".png'/></div>\n"
               + "<div class='stats'>\n"
               + "<div class='name'><span class='number'>" + objUnpicked[i].Num + "</span> <span class='first'>" + objUnpicked[i].First + "</span> <span class='last'>" + objUnpicked[i].Last + "</span><span class='suffix'> " + objUnpicked[i].Suffix + "</span> <span class='position'>- " + objUnpicked[i].Position + "</span></div>\n"
               + "<div class='resume'>College: <span class='college'>" + objUnpicked[i].College + "</span> &nbsp;&nbsp;Yrs. Exp.: <span class='years-experience'>" + objUnpicked[i].Experience + "</span></div>\n"
               + "<div class='physical'><span class='height'>" + objUnpicked[i].Height + "</span> <span class='weight'>" + objUnpicked[i].Weight + "</div>\n"
               + "<div class='summary'>" + objUnpicked[i].Summary + "</div>\n"
               + "</div>\n"
               + "<button class='draft' value='" + objUnpicked[i].PlayerID + "'>DRAFT</button>\n"
               + "</div>\n"; 
      }
      else {
        if (objUnpicked[i].Position == thisPosition) { //otherwise, if they want to see only players of a certain position, only display players with the clicked-on position
          appendString += "<div class='player'>\n"
                 + "<div class='headshot'><img src='img/" + objUnpicked[i].Photo + ".png'/></div>\n"
                 + "<div class='stats'>\n"
                 + "<div class='name'><span class='number'>" + objUnpicked[i].Num + "</span> <span class='first'>" + objUnpicked[i].First + "</span> <span class='last'>" + objUnpicked[i].Last + "</span><span class='suffix'> " + objUnpicked[i].Suffix + "</span> <span class='position'>- " + objUnpicked[i].Position + "</span></div>\n"
                 + "<div class='resume'>College: <span class='college'>" + objUnpicked[i].College + "</span> &nbsp;&nbsp;Yrs Exp: <span class='years-experience'>" + objUnpicked[i].Experience + "</span></div>\n"
                 + "<div class='physical'><span class='height'>" + objUnpicked[i].Height + "</span> <span class='weight'>" + objUnpicked[i].Weight + "</div>\n"
                 + "<div class='summary'>" + objUnpicked[i].Summary + "</div>\n"
                 + "</div>\n"
                 + "<button class='draft' value='" + objUnpicked[i].PlayerID + "'>DRAFT</button>\n"
                 + "</div>\n"; 

        }
      }
    }
    $('.players').append(appendString); //fill in the players div

     $('.draft').on("click", function() { //if click the draft button associated with each player
      alert('clicked draft'); //this works
      var thisGuysPosition = "";

      var thisPlayer = $(this).attr("value"); //get the ID of the clicked player

      //find this player's position in the array
      for (var j = 0; j< objUnpicked.length; j++) {
        if (objUnpicked[j].PlayerID == thisPlayer) {
          thisGuysPosition = objUnpicked[j].Position;
        }
      }
      alert(thisGuysPosition); //this works
      var clickedIndex;
      var countQB = 0; //we haven't picked anybody yet, so counts are 0
      var countWR = 0; var countRB = 0; var countTE = 0; var countOL = 0; var countDL = 0; var countLB = 0; var countCB = 0; var countS = 0; var countSpecial = 0;

      if (thisGuysPosition == "QB") {
        alert('counting now'); //this works
        countQB++; 
        var QBleft = 3-countQB;
        alert($('.instructions').html()); //this works
                //the following two statements only work the first time 
                //they don't update on subsequent times. Why?
        $('.numLeft').text("");
        $('.numLeft').text("You have " + QBleft + " QBs left."); 
      }
     }); //end click draft button 
} //end getPosition function

个字符

sh7euo9m

sh7euo9m1#

您的代码工作正常,它更新文本也很好。您的问题是您在draft按钮的单击事件中定义了var countQB = 0;。每次单击它,您都会将计数重置为零。您只需要在单击事件之外定义它,这样就可以跟踪两次单击之间的次数。
下一次你写一个问题时,请只包含一个 * 最小 * 数量的代码,可以重现这个问题。你的问题是一个巨大的代码块,有太多的样本数据。
见我下面的编辑。我删除了大量的样本数据,因为不需要重现手头的问题。我还定义了一些用于选择元素的新变量,这样您就不必反复选择它们

//Select some elements to be re-used
var $instructions = $('.instructions')
var $players = $('.players');
var $numLeft = $('.numLeft');

//application state
var countQB = 0;

//Sample data
var objUnpicked = [
  {PlayerID: "1",Num: "57",First: "Montravius",Last: "Adams",Suffix: "",Position: "DL",Photo: "adams-montravius",Height: "6-4",Weight: "304",College: "Auburn",Experience: "7",Summary: "Nose tackle played 26% of the defensive snaps.",},
  {PlayerID: "9",Num: "5",First: "Tanner",Last: "Morgan",Suffix: "",Position: "QB",Photo: "morgan-tanner",Height: "6-2",Weight: "215 ",College: "Minnesota",Experience: "R",Summary: "Up-and-down 5-year starter in Big Ten went undrafted.",},
  {PlayerID: "11",Num: "8",First: "Kenny",Last: "Pickett",Suffix: "",Position: "QB",Photo: "pickett-kenny",Height: "6-3",Weight: "220",College: "Pitt",Experience: "2",Summary: "Tossed 5 TDs to just 1 INT over final 8 games.",},
  {PlayerID: "46",Num: "42 ",First: "James",Last: "Pierre",Suffix: "",Position: "CB",Photo: "pierre-james",Height: "6-2",Weight: "185",College: "Fla. Atlantic",Experience: "4",Summary: "Job in jeopardy as young studs join his group.",},
];
getPosition("ALL");

function getPosition(thisPosition) {
    //reflect instructions for each position
    $instructions.empty();
    switch(thisPosition) {
      case 'QB':
        $instructions.html('Pick a maximum of 3 QBs.');
        break;
      case 'RB':
        $instructions.html('Pick a maximum of 5 RBs.');
        break;
      case 'WR':
        $instructions.html('Pick a maximum of 7 WRs.');
        break;
      case 'TE':
        $instructions.html('Pick a maximum of 4 TEs.');
        break;
      case 'OL':
        $instructions.html('Pick a maximum of 9 OLs.');
        break;
      case 'DL':
        $instructions.html('Pick a maximum of 9 DLs.');
        break;
      case 'LB':
        $instructions.html('Pick a maximum of 8 LBs.');
        break;
      case 'CB':
        $instructions.html('Pick a maximum of 7 CBs.');
        break;
      case 'S':
        $instructions.html('Pick a maximum of 5 Ss.');
        break;
      case 'Special':
        $instructions.html('Pick a maximum of 3 special team members.');
        break;
      case 'ALL':
        $instructions.html('');
        break;
      default:
      // code block
    }

    $players.empty(); //get rid of any former players showing on the web page
    var appendString = ""; //initialize string of html of player data

    //look through unpicked players object and return those that match clicked position
    for (var i = 0; i< objUnpicked.length; i++) { //go through the object of unpicked players
      var pick = objUnpicked[i];
      if (thisPosition == "ALL") { //if they want to display all players, display them all
        appendString += "<div class='player'>\n"
               + "<div class='headshot'><img src='img/" + pick.Photo + ".png'/></div>\n"
               + "<div class='stats'>\n"
               + "<div class='name'><span class='number'>" + pick.Num + "</span> <span class='first'>" + pick.First + "</span> <span class='last'>" + pick.Last + "</span><span class='suffix'> " + pick.Suffix + "</span> <span class='position'>- " + pick.Position + "</span></div>\n"
               + "<div class='resume'>College: <span class='college'>" + pick.College + "</span> &nbsp;&nbsp;Yrs. Exp.: <span class='years-experience'>" + pick.Experience + "</span></div>\n"
               + "<div class='physical'><span class='height'>" + pick.Height + "</span> <span class='weight'>" + pick.Weight + "</div>\n"
               + "<div class='summary'>" + pick.Summary + "</div>\n"
               + "</div>\n"
               + "<button class='draft' value='" + pick.PlayerID + "'>DRAFT</button>\n"
               + "</div>\n"; 
      } else {
        if (pick.Position == thisPosition) { //otherwise, if they want to see only players of a certain position, only display players with the clicked-on position
          appendString += "<div class='player'>\n"
                 + "<div class='headshot'><img src='img/" + pick.Photo + ".png'/></div>\n"
                 + "<div class='stats'>\n"
                 + "<div class='name'><span class='number'>" + pick.Num + "</span> <span class='first'>" + pick.First + "</span> <span class='last'>" + pick.Last + "</span><span class='suffix'> " + pick.Suffix + "</span> <span class='position'>- " + pick.Position + "</span></div>\n"
                 + "<div class='resume'>College: <span class='college'>" + pick.College + "</span> &nbsp;&nbsp;Yrs Exp: <span class='years-experience'>" + pick.Experience + "</span></div>\n"
                 + "<div class='physical'><span class='height'>" + pick.Height + "</span> <span class='weight'>" + pick.Weight + "</div>\n"
                 + "<div class='summary'>" + pick.Summary + "</div>\n"
                 + "</div>\n"
                 + "<button class='draft' value='" + pick.PlayerID + "'>DRAFT</button>\n"
                 + "</div>\n"; 
        }
      }
    }
    $players.html(appendString); //fill in the players div

    $('.draft').on("click", function() { //if click the draft button associated with each player
      var thisGuysPosition = "";
      var thisPlayer = $(this).attr("value"); //get the ID of the clicked player

      //find this player's position in the array
      for (var j = 0; j< objUnpicked.length; j++) {
        if (objUnpicked[j].PlayerID == thisPlayer) {
          thisGuysPosition = objUnpicked[j].Position;
        }
      }

      if (thisGuysPosition == "QB") {
        countQB++; 
        var QBleft = 3-countQB;
        console.log('counting QBs', countQB, QBleft);
        $numLeft.text("You have " + QBleft + " QBs left."); 
      }
    }); //end click draft button 
} //end getPosition function

个字符

相关问题