开发者

Disable submit button until hidden field has content

开发者 https://www.devze.com 2023-02-02 08:48 出处:网络
I have a hidden f开发者_JAVA技巧ield that is populated when the user does a specific AJAX call.I want to find a way to disable the submit button until the hidden field is populated with content.

I have a hidden f开发者_JAVA技巧ield that is populated when the user does a specific AJAX call. I want to find a way to disable the submit button until the hidden field is populated with content.

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


Just set it to disabled and enable it at the same place you're setting the hidden value. I think you're trying to make it harder than it is.


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


Initially set:

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

After your ajax call:

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


Try this:

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

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

Just make sure your Ajax call triggers the change event of the hidden inputs.


I usually do the following:

In with the rest of my JavaScript:

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

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

Then, in my HTML markup:

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

精彩评论

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