开发者

Why is FBJS dropping my 'onclick' attribute from this input element?

开发者 https://www.devze.com 2022-12-09 00:11 出处:网络
When I include an \'onClick\' attribute in setInnerXTHML() like this: var innerHtml = \'<span>Build Track: Select a city where track building should begin\'+

When I include an 'onClick' attribute in setInnerXTHML() like this:

var innerHtml = '<span>Build Track: Select a city where track building should begin'+
                '<div id="action-end">'+
                '<form>'+
                '<input type="button" value="End Track Building" id="next-phase" onClick="moveTrainAuto();" />'+
                '</form>'+
                '</div></span>';
actionPrompt.setInnerXHTML(innerHtml);
var btn = document.getElementById('next-phase');
btn.addEventListener('click', 'moveTrainAuto开发者_开发知识库');

The 'onClick' gets dropped. I know this from inspecting the element with Firebug. This is what it reveals:

<input id="app148184604666_next-phase" type="button" value="End Track Building"/>

Here is the function called for onClick:

function moveTrainAuto(evt) {
debugger;
  ajax = new Ajax();
  ajax.responseType = Ajax.JSON;
  ajax.ondone = function(data) {
debugger;
    if(data.code == 'UNLOAD_CARGO') {
      unloadCargo();
    } else if (data.code == 'MOVE_TRAIN_AUTO') {
      moveTrainAuto();
    } else if (data.code == 'TURN_END') {
      turnEnd();
    } else {
      /* handle error */
    }
  }
  ajax.post(baseURL + '/turn/move-trains-auto');
}

As you can see, I've tried both 'onClick' and 'addEventListener'. Neither works. Any ideas?


Instead of passing in the function name, try passing in the function.

btn.onClick = moveTrainAuto;

not

btn.onClick = 'moveTrainAuto';
0

精彩评论

暂无评论...
验证码 换一张
取 消