, 당신의 인생을 촉진하기 위해 자바 스크립트를 작성할 때 jQuery 사용 감사드립니다.
을 : 염두에두고 있다는 점에서
은의 당신이해야합니다 그래서 마지막에 당신이
list-item
명명되어 있다고 가정 해 봅시다, 당신은 클래스를 가지고 있는지 확인 ...의 당신의
ListItem
컨트롤이
id
와
ul
목록을 출력한다고 가정하자 이제
<div id="ListItem_1" class="list-item">
...
</div>
, 당신은
<div id="ListItem_1" class="list-item>
<input id="ListItem_1_Button_1" type="button" value="I'm a Button" />
</div>
다시 한번, 명시 적으로 예를 들어, 그 버튼 클래스 이름을 ... 당신이 목록 안에 버튼 어떤 종류의 말 :
inside-button
난 당신이 모든 텍스트를 얼마나 잘 모르겠지만, 내가 다시 그에게 경고 할 텍스트와 hiiden
이 필드를 가정합니다 ...
어떤 것을 같은 :
<div id="ListItem_1" class="list-item>
<input id="ListItem_1_Button_1" type="button" value="I'm a Button" />
<input id="ListItem_1_Hidden_1" type="hidden" class="text" value="Text to run" />
</div>
당신은 몇 가지 버튼 (10 개) 목록을 가정 할 수 있습니다 간단한 쓰기 : 1의 선은 말한다
$(".inside-button").bind("click", function() {
// a button was clicked, let's do something
});
것을 "inside-button
의 클래스 이름에있다의 Foreach DOM 요소를
클릭 이벤트 "를 타코미터 당신은 당신이 당신의 질문에서
를 수행 할 작업을 해고 안에, 당신은 단지 그 목록에 무엇인가를 수행 할 :
$(this) // that will be the .inside-button element, so the Button
.closest(".list-item") // get me the closest DOM element with a class name of "list-item"
.find(".text") // find the DOM elemnt with the class name "text"
.val(); // let's see what value that element has
그때 당신이 그것을 alert()
수 있습니다. 모두 함께
:
$(".inside-button").bind("click", function() {
// a button was clicked, let's do something
var txt = $(this).closest(".list-item").find("text").val();
alert(txt);
});