2013-12-13 2 views
0

나는이 코드로 내가 뭘 잘못했는지 알아 내려고하면서 유성에 속고있다. 유성이 다른 것과 관련이 있거나 내 jQuery를 방금 끄집어 냈습니까? 그리고 나는 그것이 내가 여기에서 일어나고 싶은 것이 명백하다고 생각합니다. 그러나 당신이 이해하지 못한다면 나는 이것을 ("그다지 만지지 마십시오") 버튼 클릭으로 경고하고 싶습니다.이 클릭 이벤트가 잘못되었습니다.

jQuery를 :

$(document).ready(function(){ 
    $("button").click(function(){ 
    alert("dont touch that"); 
    }); 
}); 

HTML :

<head> 
    <title>bubblePop</title> 
</head> 

<body> 
    {{> hello}} 
</body> 

<template name="hello"> 
    <h1>Hello World!</h1> 
    {{greeting}} 
    <input type="button" value="Clic" /> 
</template> 

답변

2
Template.hello.events({ 
    "click [type='button']": function (evt, template) { 
    alert("don't touch that"); 
    } 
}); 
+1

+1이 템플릿은 템플릿이 여러 개있는 경우에도 계속 작동하므로 JQuery를 사용하는 것보다 낫습니다. 'document.ready'가 발생한 후에 템플릿이 렌더링되기 때문에 op의 문제도 해결합니다. – Akshat

1
당신이 동적 템플릿을 사용하여 요소를 생성하기 때문에

, 그래서 jQuery를에 event delegation

$(document).on('click', "input[type="button"]", function() { 
    alert("dont touch that"); 
}); 
+0

대안 :'$ ("button"). on ('click', function() {alert ("dont touch that that");});'. – bredikhin

1

선택기를 사용하는 CSS입니다 -ish. 그것은 단지 문자열 일 때 해당 유형의 요소를 찾습니다. 따라서 "button"을 전달하면 버튼 요소 (아무 것도없는)를 찾게되고 결과적으로 핸들러가 연결되지 않습니다.

당신은 아마 당신은 $('input'), 또는 $('input[type="button"]')을 사용하거나 버튼 ID와 참조를 줄 수있는 당신의 입력 요소

$(document).ready(function(){ 
$("input").click(function(){ 
    alert("dont touch that"); 
}); 
}); 
1

을 대상으로하고 싶어한다는로 jQuery를 선택 일명 $('#ButtonID')

관련 문제