本文共 2144 字,大约阅读时间需要 7 分钟。
本文翻译自:
I have two radio button on change event i want change button How it is possible? 我在更改事件上有两个单选按钮我想要更改按钮怎么可能? My Code 我的密码
AllotTransfer
Script 脚本
参考:
You can use this
which refers to the current input
element. 您可以使用this
它指的是当前input
元素。
$('input[type=radio][name=bedStatus]').change(function() { if (this.value == 'allot') { alert("Allot Thai Gayo Bhai"); } else if (this.value == 'transfer') { alert("Transfer Thai Gayo"); }});
Note that you are comparing the value against allot
in both if statements and :radio
selector is deprecated. 请注意,您已在if语句和:radio
选择器均已弃用的情况下,将值与allot
比较。
In case that you are not using jQuery, you can use the document.querySelectorAll
and HTMLElement.addEventListener
methods: 如果您不使用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);});
An adaptation of the above answer... 以上答案的改编...
$('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; } });
Allot Transfer
$(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"); } });});
A simpler and cleaner way would be to use a class with @Ohgodwhy's answer 一种更简单,更简洁的方法是将类与@Ohgodwhy的答案一起使用
Script 脚本
$( ".rButton" ).change(function() { switch($(this).val()) { case 'allot' : alert("Allot Thai Gayo Bhai"); break; case 'transfer' : alert("Transfer Thai Gayo"); break; } });
upiBank
转载地址:http://kfexb.baihongyu.com/