How do I enable an onclick event in a link on a form?
You don't need the #href to prevent the event.  

$form['test'] = [
  '#type' =&gt; 'link',
  '#href' =&gt; '#',
  '#title' =&gt; t('Show'),
  '#attributes' =&gt; [
    'onclick' =&gt; "alert('hello');return false",
  ],
  '#id' =&gt; 'myid',
];


In the above example, I removed the #href and the onsubmit handler and added return false after the alert() function in the onclick handler.
I don't know what  you're trying to achieve in your code, but the above example is working for me.  



UPDATE:
I update the #href in my answer.