如何使用 Jquery 将类的 id 获取到 Web 服务中

发布于 2024-10-16 07:13:37 字数 474 浏览 4 评论 0原文

我有几个“就地编辑”文本框位于生成的表中,我需要将标识符从页面传递到网络服务,因此...

$(document).ready(function () {

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

由于我有几个相同的框,我需要按类调用并且不是通过元素,但我需要从单个元素中获取 Id 属性“rel”并将其传递到 Web 服务。在匿名函数 (function() { return $(this).attr("rel")},) 的最后一点,我总是陷入困境。我已经尝试了很多变体,如果我对某些变体发出警报,它会给我正确的结果,但不会返回它。请帮忙。

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 () {

 $(".extraText").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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Bonjour°[大白 2024-10-23 07:13:37

我想如果 editInPlace 不提供任何在运行时获取值的可能性,您必须循环遍历每个元素:(

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

直接函数只是为了不使用 url< /code> 变量并避免重复的字符串连接)

不确定您想要使用匿名函数实现什么......就目前而言,它只是尝试将函数与字符串连接起来。

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.

三生路 2024-10-23 07:13:37

你的函数永远不会被调用。当您编写如下内容时:

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

Javascript 会将“Abc”与 function() { return 'def'; 的求值连接起来} 这是一个函数对象!
它相当于:

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

Javascript 将 foo 转换为字符串,结果将是:

"Abc [Object]"

因为 Function 对象的字符串表示形式是 '[Object]' (或者可能是 "Abc function() { return 'def'; }",这取决于你的 JS 实现)。

这与以下内容略有不同:

"Abc " + foo();

这是一个函数调用执行函数,并给出预期结果:

"Abc def"

按照 Felix Kling 的建议找到问题的解决方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文