简体   繁体   English

如果启用了Javascript,则使用onclick事件替换提交按钮

[英]Replace submit button with onclick event if Javascript enabled

Keeping with the idea of progressive implementation, I have a simple page refresh button on my page. 保持渐进式实现的想法,我的页面上有一个简单的页面刷新按钮。 Is there an elegant way to turn that button into an onclick="ajaxfunction()" event if javascript is enabled? 如果启用了javascript,是否有一种优雅的方法可以将该按钮变为onclick =“ajaxfunction()”事件?

All I've come up with having two buttons, and hiding one with CSS -- which seems a bit clunky. 所有我都想出了两个按钮,并用CSS隐藏了一个 - 这看起来有点笨重。

Note, I have the ajax call working, etc - I just want that $%@# button to switch over... 注意,我有ajax调用工作等 - 我只想要$%@#按钮切换...

Attach an event listener to the submit button which listens to the click event and cancels the event. 将事件侦听器附加到提交按钮,该按钮用于侦听click事件并取消事件。 With prototype it would be something like this: 使用原型,它将是这样的:

document.observe('dom:loaded', function(){
    $('mysubmitbuttonid').observe('click',function(event){
        new Ajax.Request(...);
        Event.stop(event);
        return false;
    });
});

Assuming you've a form with a button type of submit : 假设你一个formbutton类型的submit

<button id="sbut" type="submit" ...>OK</button>

You can do this: 你可以这样做:

function chgButton () {
    var button = document.getElementById('sbut');
    button.type = 'button';
    button.addEventListener('click', ajaxfunction,false);
    return;
}

window.onload = function () {
    chgButton();
    return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM