簡體   English   中英

禁用提交按鈕,直到隱藏字段包含內容

[英]Disable submit button until hidden field has content

我有一個隱藏字段,當用戶執行特定的AJAX調用時會填充該字段。 我想找到一種方法來禁用提交按鈕,直到用內容填充隱藏字段為止。

   <input  type="hidden" name="lat" id="t1">
    <input type="hidden" name="long" id="t2"> 

只需將其設置為禁用,然后在設置隱藏值的同一位置啟用它即可。 我認為您正在嘗試使難度變得更大。

$('#yourForm').submit(function() {
    if ( !$('#t1')[0].value && !$('#t2')[0].value ) { return false; }
});

初始設置:

<input id="GO" type="submit" style="display: none" />

在您的ajax調用之后:

$('#GO').show();

嘗試這個:

$('input[type="submit"]').attr('disabled', 'disabled');

$('#t1, #t2').change(function() {
    var enable = $('#t1').val() && $('#t2').val();
    $('input[type="submit"]').attr('disabled', enable ? null : 'disabled');
});

只需確保您的Ajax調用觸發了隱藏輸入的change事件。

我通常會執行以下操作:

在我的其余JavaScript中:

$.ajax({ //ajax options go here
    success: function(result) {
        //Do the rest of your success stuff here.

        $("#submit").removeAttr("disabled");
    }
});

然后,在我的HTML標記中:

<button type="submit" name="submit" id="submit" disabled="true">Submit</button>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM