Javascript Dojo/Dijit单选按钮单击时事件在再次单击同一按钮时不会激发

hjzp0vay  于 2022-12-20  发布在  Dojo
关注(0)|答案(1)|浏览(301)

我可以创建带有on-click事件的Dijit单选按钮,它们工作正常。但是,当同一个按钮--即当前选中的按钮--再次被单击时,on-click事件不会触发。我如何才能使当前选中的单选按钮再次被单击时,有一个事件回调函数可以使用呢?
我正在使用dijit/form/单选按钮”。
我按如下编程方式创建单选按钮:

var aRadioButton = new RadioButton({
  checked: someBooleanValue,
  value: someValue,
  name: someName,
  onClick: lang.hitch(this, buttonCallback) // This works, except for when same button is clicked.
});
xriantvc

xriantvc1#

默认情况下,Dojo不支持RadioButton的这种行为。
作为_RadioButtonMixin.js文件中函数_onClick的猴子路径的替代,您可以直接在dom本身上添加一个事件侦听器,在本例中,我在RadioButton上使用focusNode
示例:
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>

相关问题