如何使用 Jquery 将类的 id 获取到 Web 服务中
我有几个“就地编辑”文本框位于生成的表中,我需要将标识符从页面传递到网络服务,因此...
$(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想如果
editInPlace
不提供任何在运行时获取值的可能性,您必须循环遍历每个元素:(直接函数只是为了不使用
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:(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.
你的函数永远不会被调用。当您编写如下内容时:
Javascript 会将“Abc”与
function() { return 'def'; 的求值连接起来}
这是一个函数对象!它相当于:
Javascript 将
foo
转换为字符串,结果将是:因为 Function 对象的字符串表示形式是 '[Object]' (或者可能是
"Abc function() { return 'def'; }"
,这取决于你的 JS 实现)。这与以下内容略有不同:
这是一个函数调用并执行函数,并给出预期结果:
按照 Felix Kling 的建议找到问题的解决方案。
Your function is never called. When you write something like:
Javascript will concatenate "Abc " with the evaluation of
function() { return 'def'; }
which is a Function object!It's equivalent to:
Javascript will convert
foo
into a string, and the result will be: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:
which is a function call and executes the function, and gives the expected result:
Follow the recomendation of Felix Kling to find a solution to your problem.