开发者

How to get the id off a class into a webservice using Jquery

I have several "edit in Place" text boxes that sit within a generated table and I need to pass an identifier from the page to a webservice, thus...

$(document).ready(function () {

 $(".extra开发者_运维问答Text").editInPlace({
            url: "http://" + document.location.host + "/MenuEdit/EditExtra/",
            params: "Id=" + function() {return $(this).attr("rel")},
etc

As I have several, of the same boxes I need to call by class and not by Element, but I need to get the Id attribute "rel" off the individual element and pass this through to the web service. At the last point of the anonymous function (function() { return $(this).attr("rel")},) I keep coming unstuck. I have tried many variations of this and if I put an alert in on some variants and it will give me the correct result, but wont return it. Please help.


I guess you have to loop over each element if editInPlace does not provide any possibility to to get a value at runtime:

(function() {
    var url = "http://" + document.location.host + "/MenuEdit/EditExtra/";
    $(".extraText").each(function() {       
         $(this).editInPlace({
            url: url,
            params: "Id=" + $(this).attr("rel"),
        etc 
        });
    });
}());

(the immediate function is just to not pollute the global namespace with the url variable and avoiding of repetitive string concatenation)

Not sure what you want to achieve with the anonymous function... as it stands, it just tries to concatenate a function with a string.


Your function is never called. When you write something like:

"Abc " + function() { return 'def'; };

Javascript will concatenate "Abc " with the evaluation of function() { return 'def'; } which is a Function object! It's equivalent to:

var foo = function() { return 'def'; };
"Abc " + foo;

Javascript will convert foo into a string, and the result will be:

"Abc [Object]"

because the string representation of a Function object is '[Object]' (or maybe "Abc function() { return 'def'; }", it depends on your JS implementation).

This is slightly different from:

"Abc " + foo();

which is a function call and executes the function, and gives the expected result:

"Abc def"

Follow the recomendation of Felix Kling to find a solution to your problem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜