Javascript Dojo/Dijit 如果再次单击同一个按钮,将不会触发单选按钮单击事件
Javascript Dojo/Dijit Radio Button On-Click Event Won't Fire if Same Button Clicked again
我可以使用单击事件创建 Dijit 单选按钮,它们工作正常。但是,当再次单击同一个按钮(即当前选中的按钮)时,不会触发 on-click 事件。我怎样才能做到这一点,以便在再次单击当前选中的单选按钮时可以使用事件回调?
我正在使用 dijit/form/RadioButton"。
我按如下方式创建单选按钮:
var aRadioButton = new RadioButton({
checked: someBooleanValue,
value: someValue,
name: someName,
onClick: lang.hitch(this, buttonCallback) // This works, except for when same button is clicked.
});
Dojo 默认不支持 RadioButton
的这种行为。
除了文件 _RadioButtonMixin.js
中函数 _onClick
的猴子路径之外,您还可以直接在 dom 本身上添加一个事件侦听器,在此示例中我使用 focusNode
在 RadioButton
.
实例:
https://jsfiddle.net/gibbok/kwdy5gqc/
require([
"dojo/on",
"dojo/parser",
"dijit/form/RadioButton",
"dijit/form/Button", // used for example purpose
"dojo/domReady!"
], function(on, parser, RadioButton) {
var radioOne = new RadioButton({
checked: true,
value: "tea",
name: "drink"
}, "radioOne");
var radioTwo = new RadioButton({
checked: false,
value: "coffee",
name: "drink"
}, "radioTwo");
var signal = on(radioOne.focusNode, "click", function(event) {
// this code will fire when domNode is clicked
alert('click on input tea');
});
var signal = on(radioTwo.focusNode, "click", function(event) {
// this code will fire when domNode is clicked
alert('click on input cafee');
});
parser.parse();
});
<form id="myform">
<input type="radio" name="drink" id="radioOne" checked value="tea" />
<label for="radioOne">Tea</label>
<br />
<input type="radio" name="drink" id="radioTwo" value="coffee" />
<label for="radioTwo">Coffee</label>
<br />
<button data-dojo-type="dijit/form/Button" type="button">
Show form submit value
<script type="dojo/on" data-dojo-event="click">
require(["dojo/dom"], function(dom){ with(dom.byId('myform'))with(elements[0])with(elements[checked?0:1])alert(name+'='+value); return false; });
</script>
</button>
</form>
我可以使用单击事件创建 Dijit 单选按钮,它们工作正常。但是,当再次单击同一个按钮(即当前选中的按钮)时,不会触发 on-click 事件。我怎样才能做到这一点,以便在再次单击当前选中的单选按钮时可以使用事件回调?
我正在使用 dijit/form/RadioButton"。
我按如下方式创建单选按钮:
var aRadioButton = new RadioButton({
checked: someBooleanValue,
value: someValue,
name: someName,
onClick: lang.hitch(this, buttonCallback) // This works, except for when same button is clicked.
});
Dojo 默认不支持 RadioButton
的这种行为。
除了文件 _RadioButtonMixin.js
中函数 _onClick
的猴子路径之外,您还可以直接在 dom 本身上添加一个事件侦听器,在此示例中我使用 focusNode
在 RadioButton
.
实例:
https://jsfiddle.net/gibbok/kwdy5gqc/
require([
"dojo/on",
"dojo/parser",
"dijit/form/RadioButton",
"dijit/form/Button", // used for example purpose
"dojo/domReady!"
], function(on, parser, RadioButton) {
var radioOne = new RadioButton({
checked: true,
value: "tea",
name: "drink"
}, "radioOne");
var radioTwo = new RadioButton({
checked: false,
value: "coffee",
name: "drink"
}, "radioTwo");
var signal = on(radioOne.focusNode, "click", function(event) {
// this code will fire when domNode is clicked
alert('click on input tea');
});
var signal = on(radioTwo.focusNode, "click", function(event) {
// this code will fire when domNode is clicked
alert('click on input cafee');
});
parser.parse();
});
<form id="myform">
<input type="radio" name="drink" id="radioOne" checked value="tea" />
<label for="radioOne">Tea</label>
<br />
<input type="radio" name="drink" id="radioTwo" value="coffee" />
<label for="radioTwo">Coffee</label>
<br />
<button data-dojo-type="dijit/form/Button" type="button">
Show form submit value
<script type="dojo/on" data-dojo-event="click">
require(["dojo/dom"], function(dom){ with(dom.byId('myform'))with(elements[0])with(elements[checked?0:1])alert(name+'='+value); return false; });
</script>
</button>
</form>