开发者

jQuery function called by Generic Javascript function

开发者 https://www.devze.com 2023-01-25 06:16 出处:网络
I\'m a bit confused it seems here. What I\'m trying to do is create a Javascript function that disables all i开发者_JAVA技巧nputs on a page, and then a function that enables all the inputs on a page.

I'm a bit confused it seems here. What I'm trying to do is create a Javascript function that disables all i开发者_JAVA技巧nputs on a page, and then a function that enables all the inputs on a page.

This was pretty easy with jQuery. My question is, how can I wrap these jQuery functions in a traditional JS function? The reason I need to do this is because I'm calling these functions using ActionScript. Put another way, I don't understand how I can write jQuery code and then call it in a tradition Javascript function call manner.

Works great, but isn't wrapped in a function...

$(document).ready(function() {
    $(':input').attr('disabled', "disabled");
});

Wrapped in a function and doesn't work at all, what am I doing wrong here??

function init_lock_test(){
    $(':input').attr('disabled', "disabled");
}


You can't directly call JavaScript functions in a web page from ActionScript, and there's no special thing as a "jQuery function." You should be using ActionScript's ExternalInterface to call the JavaScript function indirectly:

import flash.external.ExternalInterface

// Calls the function init_lock_test
ExternalInterface.call("init_lock_test");


Personally I'd avoid using jQuery for this, because it's not really needed :p
Try this:

function lockInputs(lock) {
    var tags, elems, i, j;
    tags = ['input','button','textarea'];
    for( i=0; i<tags.length; i++) {
        elems = document.getElementsByTagName(tags[i]);
        for( j=0; j<elems.length; j++) {
            if( lock) elems[j].setAttribute("_lock",elems[j].disabled);
            elems[j].disabled = lock || elems[j].getAttribute("_lock");
        }
    }
}

That should disable all input fields with lockInputs(true); and re-enable them with lockInputs(false);
It also saves the disabled state, so any that were disabled to start with stay that way.
HTH!


Wrapping jquery within a traditional js function really is as easy as your init_lock_test function. You must have an issue with invoking your function from ActionScript. Try this to verify:

$(document).ready(function() {
  init_lock_test();
});
0

精彩评论

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