单击另一个按钮并提交表单后启用按钮

时间:2015-11-04 15:45:57

标签: jquery html css

我有两个按钮:查询和导出。默认情况下,“导出”按钮处于禁用状态,但单击“查询”按钮并且表单已加载时,我希望启用导出按钮。

现在,当我点击“查询”时,导出会启用一秒钟,然后又会重新启用。我假设在提交表单时它有一些事情要做,并将“导出”按钮恢复为原始状态。

<button type="submit" class="btn btn-primary btn-small" id="query">Query</button>
<button class="btn btn-primary btn-small" id="export-button" disabled="disabled">Export</button>

这是我的jQuery目前的样子。

$(".form").on("submit", function () {
    $('#export-button').prop('disabled', false);
})

3 个答案:

答案 0 :(得分:0)

我知道我已经发布了一段时间了,但是如果有人在这里遇到类似的问题,那就是我要解决的问题:

首先,我在表单中创建了一个隐藏的输入,以便记住按钮是否已被点击

<input type="hidden" name="clicked" value="true" />

在此功能中,如果单击按钮,它将检入会话存储,或者将其保存到会话存储。

$(function () {
    $(document).ready(function () {
    var clicked = sessionStorage.getItem('clicked');
        if (clicked == 'true') {
            $('#exportButton').removeAttr('disabled');
        } else {
            $('#exportButton').attr('disabled', 'disabled');
        }
    });
    $('#query').on("click", function () {
        sessionStorage.setItem('clicked', 'true');
    });
    $('#reset').on("click", function () {
        sessionStorage.setItem('clicked', 'false');
    });
});

答案 1 :(得分:-1)

  

jQuery 1.6 + https://api.jquery.com/removeProp/
  jQuery 1.5- https://api.jquery.com/removeAttr/

如果你有 jQuery 1.6 + ,请尝试

$('#export-button').removeProp('disabled');

如果你有 jQuery 1.5 或更低版本,请尝试使用:

$('#export-button').removeAttr('disabled');

并确保您的表单包含课程.form

答案 2 :(得分:-1)

你可以通过这种方式实现它。假设您使用的是带有默认get方法的表单标记。

  <form>
    <button type="submit" name="submit" class="btn btn-primary btn-small" id="query">Query</button>
    <?php 
     if(empty($_GET)) 
        $str = 'disabled="disabled"';
     else 
        $str ="";
    ?>
     <button class="btn btn-primary btn-small" id="export-button" <?php echo $str; ?>>Export</button>
  </form>
相关问题