Tôi có hai
Yes
No
1, tùy thuộc vào số lần nhấp mà người dùng ____11, tôi cần gọi chức năng khác, tuy nhiên, tôi không phải là người nhận được các giá trị nhãn
Yes
No
1.Đây là mã của tôi:
function getLabel[id]
{
return $["#"+id].next[].text[];
}
function BillStatus[]{
console.log["its here"];
var label=getLabel['checkboxid1'];
console.log[label];
if[label=="No"] {
//some function to call here
}else{
//other function to call here
}
}
Yes
No
Hỏi ngày 5 tháng 9 năm 2017 lúc 11:04Sep 5, 2017 at 11:04
Prasad_JoshiPrasad_JoshiPrasad_Joshi
6022 Huy hiệu vàng12 Huy hiệu bạc32 Huy hiệu Đồng2 gold badges12 silver badges32 bronze badges
1
Bạn đã có một số lỗi chính tả trong mã của mình, nhưng vấn đề chính đến từ dòng trả về, bạn nên sử dụng
Yes
No
4 thay vì
Yes
No
5: return $["#"+id].parent[].text[].trim[];
LƯU Ý: Sử dụng
6 để loại bỏ các khoảng trống thêm từ văn bản nhãn được trả về. Use
Yes
No
6 to remove the extra spaces from the label text returned.Hi vọng điêu nay co ich.
Vì bạn đang sử dụng jQuery, giải pháp thay thế sẽ đính kèm sự kiện nhấp vào lớp chung và lấy nhãn từ một lần nhấp chuột như ví dụ dưới đây.
$['.checkbox-container input:checkbox'].on['click', function[]
{
if[ $[this].parent[].text[].trim[] === "No"]{
console.log["Nooooo"];
}else{
console.log["Yessss"];
}
}];
Yes
No
Đã trả lời ngày 5 tháng 9 năm 2017 lúc 11:12Sep 5, 2017 at 11:12
Zakaria Acharkizakaria acharkiZakaria Acharki
66K15 Huy hiệu vàng73 Huy hiệu bạc99 Huy hiệu Đồng15 gold badges73 silver badges99 bronze badges
2
Bạn cần thêm từ khóa này làm tham số vào các hàm Billstatus của bạn.this keyword as parameter to your BillStatus functions.
Thay vì GetLabel, bạn có thể viết:getLabel you can simply write:
ele.parentElement.textContent.trim[];
Bạn cần sử dụng sự kiện Onchange thay vì Onclick.onchange event instead of title.
Phiên bản JavaScript đầy đủ, không có mã nội tuyến là:
document.addEventListener['DOMContentLoaded', function[e] {
document.querySelectorAll['div[id^=check] [type="checkbox"]'].forEach[function[ele, idx] {
ele.addEventListener['change', function[e] {
var label = this.parentElement.textContent.trim[];
console.log['Label: ' + label + ' Checked: ' + this.checked];
if[label=="No"] {
//some function to call here
}else{
//other function to call here
}
}]
}]
}]
Đã trả lời ngày 5 tháng 9 năm 2017 lúc 11:25Sep 5, 2017 at 11:25
Bạn có thể thử
Yes
No
7function getLabel[id]
{
return $["#"+id].next[].text[];
}
function BillStatus[]{
console.log["its here"];
var label = $["input[type='checkbox']:checked:last"].next[].text[];
label = $.trim[label];
if[label== "No"] {
console.log["No is checked"];
}else if[label== "Yes"] {
console.log["Yes is checked"];
}
}
Yes
No
Đã trả lời ngày 5 tháng 9 năm 2017 lúc 11:33Sep 5, 2017 at 11:33
Tôi nghĩ rằng bạn đang chọn phần tử DOM tiếp theo thay vì phần tử cha. Xin vui lòng tham khảo thùng này.
function getLabel[id]
{
return $["#"+id].parent[].text[].trim[];
}
function BillStatus[]{
var elem=this.event.target.id;
var label=getLabel[elem];
console.log[label];
if[label=="No"] {
alert[label]
}else{
alert[label]
}
}
Yes
No
0Đã trả lời ngày 5 tháng 9 năm 2017 lúc 11:47Sep 5, 2017 at 11:47