[英]How to handle when a radio button is clicked?
我希望在單擊單選按鈕時調用 function 。 在這個 function 中會有一個二維數組,格式如下:
[[0,0],[1,0],[2,1],[3,0],[4,1]]
數組條目將如下所示: [regionNumber, 0 or 1 ]
regionNumber
對應於從0
開始的區域編號0
是對應的單選按鈕沒有被點擊。1
是相應的單選按鈕已被單擊。我會將這個二維數組傳遞給另一個組件來使用。
當單選按鈕被點擊時,它會在二維數組中被識別,並且對應的0/1
會被切換為相反的值。
例如:
// this means `regionNumber` 2 and `regionNumber` 4 radio buttons are checked.
[ [0,0], [1,0], [2,1], [3,0], [4,1] ]
// if we click the radio button 4 again (`regionNumber` 4) then it will turn into:
[ [0,0] , [1,0] , [2,1] , [3,0] , [4,0] ]
選中單選按鈕后,將數組的 object 發送到 Graph。 例如,當檢查[object1,object2,object3] = object1
和object2
時,它們將完成此操作。
import React from 'react';
import { MDBFormInline } from 'mdbreact';
import { MDBBtn } from "mdbreact";
import { Container } from 'reactstrap';
import $ from "jquery";
const Test = props => {
const total_regions = (JSON.parse(JSON.stringify(props.test)).length); // gets the number of regions
return (
// displays radio buttons depending on the number of objects in json
<div>
{props.test.map((item, idx) => {
return (
<label key={idx}>
<input className="region" type="radio" value={idx} />
<span>{idx}</span>
</label>
);
})}
</div>
);
};
export default Test;
I was thinking of doing a jQuery but because ill be handling an array within the function I was unsure if jQuery could do this as I also will call another component within the function.
我嘗試在單選按鈕中使用onClick
但我認為我沒有正確使用它。
有人提前感謝您的指導嗎?
只需使用onClick
。 這是一個你應該能夠適應的例子。
const Test = props => {
const total_regions = JSON.parse(JSON.stringify(props.test)).length; // gets the number of regions
const handleClick = (item, idx) => {
console.log(`item ${item} with index ${idx} clicked`);
};
return (
// displays radio buttons depending on the number of objects in json
<div>
{props.test.map((item, idx) => {
return (
<label key={idx}>
<input
className="region"
type="radio"
value={idx}
onClick={() => handleClick(item, idx)}
/>
<span>{idx}</span>
</label>
);
})}
</div>
);
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.