반응형
변경 시 라디오 사용 방법
변경 이벤트에 두 개의 라디오 버튼이 있습니다. 변경 버튼을 원합니다. 어떻게 가능합니까?내 코드
<input type="radio" name="bedStatus" id="allot" checked="checked" value="allot">Allot
<input type="radio" name="bedStatus" id="transfer" value="transfer">Transfer
대본
<script>
$(document).ready(function () {
$('input:radio[name=bedStatus]:checked').change(function () {
if ($("input[name='bedStatus']:checked").val() == 'allot') {
alert("Allot Thai Gayo Bhai");
}
if ($("input[name='bedStatus']:checked").val() == 'transfer') {
alert("Transfer Thai Gayo");
}
});
});
</script>
사용할 수 있습니다.this
그것은 현재를 의미합니다.input
원소의
$('input[type=radio][name=bedStatus]').change(function() {
if (this.value == 'allot') {
// ...
}
else if (this.value == 'transfer') {
// ...
}
});
값을 비교하는 중입니다.allot
두 가지 모두에서 만약 진술과:radio
선택기가 더 이상 사용되지 않습니다.
jQuery를 사용하지 않는 경우에는document.querySelectorAll
그리고.HTMLElement.addEventListener
메서드:
var radios = document.querySelectorAll('input[type=radio][name="bedStatus"]');
function changeHandler(event) {
if ( this.value === 'allot' ) {
console.log('value', 'allot');
} else if ( this.value === 'transfer' ) {
console.log('value', 'transfer');
}
}
Array.prototype.forEach.call(radios, function(radio) {
radio.addEventListener('change', changeHandler);
});
위의 대답을 각색한 것은...
$('input[type=radio][name=bedStatus]').on('change', function() {
switch ($(this).val()) {
case 'allot':
alert("Allot Thai Gayo Bhai");
break;
case 'transfer':
alert("Transfer Thai Gayo");
break;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" name="bedStatus" id="allot" checked="checked" value="allot">Allot
<input type="radio" name="bedStatus" id="transfer" value="transfer">Transfer
더 간단하고 깨끗한 방법은 @Oh god why's answer로 수업을 이용하는 것입니다.
<input ... class="rButton">
<input ... class="rButton">
대본
$( ".rButton" ).change(function() {
switch($(this).val()) {
case 'allot' :
alert("Allot Thai Gayo Bhai");
break;
case 'transfer' :
alert("Transfer Thai Gayo");
break;
}
});
$(document).ready(function () {
$('#allot').click(function () {
if ($(this).is(':checked')) {
alert("Allot Thai Gayo Bhai");
}
});
$('#transfer').click(function () {
if ($(this).is(':checked')) {
alert("Transfer Thai Gayo");
}
});
});
간단한 ES6(javascript 전용) 솔루션.
document.forms.demo.bedStatus.forEach(radio => {
radio.addEventListener('change', () => {
alert(`${document.forms.demo.bedStatus.value} Thai Gayo`);
})
});
<form name="demo">
<input type="radio" name="bedStatus" value="Allot" checked>Allot
<input type="radio" name="bedStatus" value="Transfer">Transfer
</form>
온체이지 함수 사용
function my_function(val){
alert(val);
}
<input type="radio" name="bedStatus" value="allot" onchange="my_function('allot')" checked="checked">Allot
<input type="radio" name="bedStatus" value="transfer" onchange="my_function('transfer')">Transfer
라디오 버튼이 동적으로 추가된 경우 이 버튼을 사용할 수 있습니다.
$(document).on('change', 'input[type=radio][name=bedStatus]', function (event) {
switch($(this).val()) {
case 'allot' :
alert("Allot Thai Gayo Bhai");
break;
case 'transfer' :
alert("Transfer Thai Gayo");
break;
}
});
document.addEventListener('DOMContentLoaded', () => {
const els = document.querySelectorAll('[name="bedStatus"]');
const capitalize = (str) =>
`${str.charAt(0).toUpperCase()}${str.slice(1)}`;
const handler = (e) => alert(
`${capitalize(e.target.value)} Thai Gayo${e.target.value === 'allot' ? ' Bhai' : ''}`
);
els.forEach((el) => {
el.addEventListener('change', handler);
});
});
<input type="radio" name="radio" value="upi">upi
<input type="radio" name="radio" value="bankAcc">Bank
<script type="text/javascript">
$(document).ready(function() {
$('input[type=radio][name=radio]').change(function() {
if (this.value == 'upi') {
//write your logic here
}
else if (this.value == 'bankAcc') {
//write your logic here
}
});
</script>
$(document).ready(function () {
$('input:radio[name=bedStatus]:checked').change(function () {
if ($("input:radio[name='bedStatus']:checked").val() == 'allot') {
alert("Allot Thai Gayo Bhai");
}
if ($("input:radio[name='bedStatus']:checked").val() == 'transfer') {
alert("Transfer Thai Gayo");
}
});
});
라디오 버튼에 클래스 "pnradio"를 추가한 다음 .attr('id')로 전환합니다.
<input type="radio" id="sampleradio1" class="pnradio" />
<input type="radio" id="sampleradio2" class="pnradio" />
$('.pnradio').click(function() {
switch ($(this).attr('id')) {
case 'sampleradio1':
alert("xxx");
break;
case 'sampleradio2':
alert("xxx");
break;
}
});
언급URL : https://stackoverflow.com/questions/13152927/how-to-use-radio-on-change-event
반응형
'sourcecode' 카테고리의 다른 글
VB의 Null 검사입니다. (0) | 2023.05.08 |
---|---|
배시 완료와 관련하여 ${array[*]} 대 ${array[@]}에 대한 혼동 (0) | 2023.05.08 |
Postgres가 수동으로 시퀀스 (0) | 2023.05.08 |
TypeError: 정수 스칼라 배열만 1D numpy 인덱스 배열로 스칼라 인덱스로 변환할 수 있습니다. (0) | 2023.05.08 |
jQuery를 사용하여 CSS 디스플레이 없음 또는 차단 속성을 변경하려면 어떻게 해야 합니까? (0) | 2023.05.08 |