2

I'm putting together a tournament bracket system allowing you to drag 'winners' to the next round, and if applicable the loser is automatically transfered to his place in a losers bracket. After each drop I'm sending to php details about the match for both individual tracking purposes as well as refilling the positions in the event it is shut down and reopened.

I ran into a problem where I was sending some unwanted characters that I used .replace() to rid myself and now everything on the winners side works fine, but anything on the losers side doesn't. Hoping someone can help me figure out the cause of this.

15:   $(window).load(function(){
16:     $(".make-draggable, .draggable, .drag").draggable({
17:         helper: "clone",
18:         snap: ".draggable",
19:         snapMode: "inner"
20:     });
21:     $(".draggable").droppable({
22:         drop: function(event, ui) {
23:             var elemText = ui.draggable.text();
24:             $(this).html(elemText);
25:             var outB = ui.draggable.attr('id').split("-");
26:             var pl1;
27:             var pl2;
28:             if (outB[0] == "go") {
29:                 var num = outB.length;
30:                 var loser;
31:                 var loserval;
32:                 var losloc;
33:                 var losid = outB[1];
34:                 var numchars = outB[1].length;
35:                 if (num === 2) {
36:                     var i = 1;
37:                     loser = (losid.charAt(0) + "-");
38:                     pl1 = elemText;
39:                    for (i = 1; i < numchars; i++) {
40:                         loser = (loser+losid.charAt(i));
41:                     }
42:                     loserval = $("#go-" + loser);
43:                     losloc = ("#"+losid);
44:                     $(losloc).html(loserval.text());
45:                     pl2 = loserval.text();
46:                 } else if (num === 3) {
47:                     loser = (outB[1] + outB[2]);
48:                     loserval = $("#go-" + loser);
49:                     losloc = ("#"+loser);
50:                     $(losloc).html(loserval.text());
51:                     pl1 = loserval.text();
52:                     pl2 = elemText;
53:                 }
54:             } else {
55:               var getround = $(this).attr('id');
56:       if(getround == 'winner') {
57:           pl1 = $('#winnerb').text();
58:            pl2 = $('#loserb').text();
59:          } else {
60:            var inti = ui.draggable.attr('id').substring(1);
61:            if (inti%2 == 0) {
62:               var apl =  inti - 1;
63:               pl1 = elemText;
64:              pl2 = $('#l'+apl).text();
65:               alert(apl);
66:            } else {
67:               var apl =  inti++;
68:               alert(apl);
69:              pl1 = $('#l'+apl).text();
70:              pl2 = elemText;
71:            }
72:          }
73:             }
74:             var matrou = ui.draggable.parent().attr("id").split("-");
75:             $.urlParam = function(name){
76:                 var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
77:             return results[1] || 0;
78:             }
79:             pl1 = pl1.replace(/(\r\n|\n|\r)/gm,"");
80:             pl2 = pl2.replace(/(\r\n|\n|\r)/gm,"");
81:             elemText = elemText.replace(/(\r\n|\n|\r)/gm,"");
82:             var params = 'win='+elemText+'&p1='+pl1+'&p2='+pl2+'&match='+matrou[3]+'&round='+matrou[1]+'&wloc='+$(this).parent().attr('id')+'-'+$(this).attr('id')+'&lloc='+$(losloc).parent().attr('id')+'-'+$(loserval).attr('id')+'&tid='+$.urlParam('tid');
83:             xmlhttp=new XMLHttpRequest();
84:             xmlhttp.onreadystatechange=function() {
85:                 if (xmlhttp.readyState==4 && xmlhttp.status==200) {
86:                 }
87:             }
88:             xmlhttp.open("POST","sub_match.php",true);
89:             xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
90:             xmlhttp.setRequestHeader("Content-length", params.length);
91:             xmlhttp.setRequestHeader("Connection", "close");
92:             xmlhttp.send(params);            
93:         }
94:     });
95:   });

For complex initialization rules inside the ctor-initializer-list, use a helper function:

class Holder {
private:
  std::unique_ptr<C1> c1;
  std::unique_ptr<C2> c2;

  static C2* c2_init_helper(/* const? */ C1& the_c1)
  {
    int const x = the_c1->getGeneratedValue1();
    int const y = the_c1->getGeneratedValue2();

    if (x > 0) {
      if (y > 0) return new C2(true, 10);
      return new C2(false, 20);
    }
    if (y > 0) return new C2(false, 30);
    return new C2(false, 0);
  }

public:
  Holder() :
    c1(new C1()),
    c2(c2_init_helper(*c1))
  {
  }
};

Also, std::unique_ptr (if you have a C++11 compiler) or boost::scoped_ptr are both preferable to std::auto_ptr. auto_ptr transfer-of-ownership copy semantics have been found to be nothing but trouble.

4

2 回答 2

1

我在手机上,所以调试代码有点困难,但我注意到在第 62 行和第 66 行,您只是获取一个元素并将其分配给 p1,而不是获取该元素的文本。尝试在该行的末尾添加 .text() 。

于 2012-06-03T16:14:05.543 回答
0

我认为符合62

  pl1 = $('l'+(inti+1));

应该

  pl1 = $('l'+(inti+1)).text();

并在线66

pl2 = $('l'+(inti+1));

应该

pl2 = $('l'+(inti+1)).text();
于 2012-06-03T17:04:22.360 回答