0
현재 으로 자바 스크립트 함수를 호출하는 내 View
에 "button"이라는 텍스트가 있습니다.고유 이름이 필요한 자바 스크립트 함수 호출
View
코드에는 PHP와 HTML, 약간의 JS가 혼합되어 있습니다.
<?php foreach($restaurant_by_legal_entities[$legal_entity_id] as $restaurant_id): ?>
<tr style="outline: thin solid">
<td class="left">
<a
id="collapse-restaurant"
data-token="<?= $token ?>"
style="text-decoration:none"
class="text"><?= $text_expand_symbol ?>
</a>
</td>
<td class="left"></td>
<td class="left"><strong>Sub-total - <?= $restaurant_name ?></</td>
<td class="left"></td>
<td class="left"></td>
<td class="left"></td>
<td class="left"></td>
<td class="left"></td>
<td class="right"><strong>¥<?= $total_by_restaurants[$restaurant_id] ?></strong></td>
</tr>
<?php endforeach ?>
내 JS 파일 :
/**
* Generate the URL to collapse the financial report to only show the totals
* only city, legal entity, and restaurant
*/
function collapse() {
console.log("Blah");
var url = commonGet('report/sale_financial', $(this).data('token'));
var collapse = $('input[name=\'collapse\']').attr('value');
if (collapse == 1) {
url += '&collapse=' + 0;
} else if (collapse == 0){
url += '&collapse=' + 1;
}
window.location = url;
}
$('#collapse-restaurant').click(collapse);
는 내가 첫 번째 단추를 클릭 할 때 (먼저 첫 번째 인덱스를 의미 함) 것으로 나타났습니다, 내 collapse
함수가 호출 될 수는 [크롬 디버거를 사용하여 테스트]. 그러나 첫 번째 인덱스 이후의 모든 것은 호출되지 않습니다. 약간의 자기 연구 끝에 각 id
은 고유해야하며 현재는 그렇지 않습니다.
id
은 .click()
마다 고유해야하므로이 문제를 해결하려면 어떻게해야합니까? 사용
ID가 고유해야합니다. ID가 아닌 클래스를 사용하십시오. – Barmar
그리고 현재 입력 값을 얻으려면'.attr ('value')'가 아니라'.val()'을 사용해야합니다. 후자는 사용자가 입력 한 것이 아니라 HTML에서 값을 가져옵니다. – Barmar
안녕하세요. @Barmar. 'class = text'를'class = collapse-restaurant'으로 바꾸는 것을 의미합니까? 나는 그것을 시도했다. 그러나 나는 지금 버튼을 놓치고있는 것처럼 보인다. – theGreenCabbage