2013-08-23 1 views
0

Demo Jsfiddle랜덤 함수와 입력 값을 어떻게 같습니까?

내 코드

HTML을

<canvas id="random" title="Click to Change"></canvas> 
<input name="rand" type="text" id="rand" data-filter="false"/> 
<span id="rands"></span> 

jQuery를

function Random() { 
     var length = 6, 
      charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 
      retVal = ""; 
     for (var i = 0, n = charset.length; i < length; ++i) { 
      retVal += charset.charAt(Math.floor(Math.random() * n)); 
     } 
     return retVal; 
    } 
    var ctx = document.getElementById("random").getContext('2d'); 
    ctx.font = "50px Arial"; 
    ctx.fillText(Random(), 0, 40); 


$("#random").click(function() { 
    if($("#rand").val() !== Random()){ 
    ctx.clearRect (0,0,1000,1000); 
    ctx.fillText(Random(), 0, 40); 
    } 
}); 



$('#rand').bind('keypress keyup change', function() { 
     if ($(this).val() !== Random()) { 
      $(this).addClass('false').removeClass('true').attr('data-filter', 'false'), $("#" + this.id + "s").css({ 
       "color": "#C41111" 
      }).text("Characters in conflict with each other!"); 
     } else { 
      $(this).addClass('true').removeClass('false').attr('data-filter', 'true'), $("#" + this.id + "s").css({ 
       "color": "#7FAC25" 
      }).text($(this).val() + " is accepted!"); 
     } 
    }); 

내가이 if($("#rand").val() !== Random())하지만 난 할 수 없습니다 동일한 싶어?

Tuerkçe olarak ben bu if içindeki functionu eşitleyemiyorum demoda görebilirinin nasıl yapacağm hakkında bilgisi olan var mı?

답변

0

영어 : Random()을 호출 할 때마다 새 값이 생성됩니다. 그래서, 여기 ($ (this) .val()! == Random())를 비교하면 새로운 값과 비교됩니다. 그래서 그들은 거의 평등하지 못할 것입니다!

세부 사항 : 울란 델리 : 랜덤() ne zaman cagirsan, yeni bir kelime üretiyorsun; dolayısıyla önceki ürettigin kelime ile deodilde klime ile karşılaştırma yapıyorsun, ki asla eşit olmaz, ancak (kullandıgınKarakterSayısı)^6 썸네일은 모두 끝내줍니다!

Neyse, ISTE çözüm :

var Deli = { 
    randomDeger:false, 
    random:function() { 
     var length = 6, 
      charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 
      retVal = ""; 
     for (var i = 0, n = charset.length; i < length; ++i) { 
      retVal += charset.charAt(Math.floor(Math.random() * n)); 
     } 
     this.randomDeger = retVal; 
     return retVal; 
    } 
} 

    var ctx = document.getElementById("random").getContext('2d'); 
    ctx.font = "50px Arial"; 
    ctx.fillText(Deli.random(), 0, 40); 


$("#random").click(function() { 
    if($("#rand").val() !== Deli.randomDeger){ 
    ctx.clearRect (0,0,1000,1000); 
    ctx.fillText(Deli.random(), 0, 40); 
    } 
}); 



$('#rand').bind('keypress keyup change', function() { 
     if ($(this).val() !== Deli.randomDeger) { 
      $(this).addClass('false').removeClass('true').attr('data-filter', 'false'), $("#" + this.id + "s").css({ 
       "color": "#C41111" 
      }).text("Characters in conflict with each other!"); 
     } else { 
      $(this).addClass('true').removeClass('false').attr('data-filter', 'true'), $("#" + this.id + "s").css({ 
       "color": "#7FAC25" 
      }).text($(this).val() + " is accepted!"); 
     } 
    }); 
+0

ASIL 델리 benim yahuu .. soru eskiymiş :) – Nihat

관련 문제