将参数从服务器对象传递到 JavaScript 的最佳方法
我渲染一个视图,其中有一个表。表的每一行都是一个可以编辑的对象。因此,该表的最后一列有一堆“编辑”按钮。单击这些编辑按钮之一时,JavaScript 函数必须获取当前行表示的对象的 Id。最终,我希望得到一个干净的 HTML:没有“onclick”、“onmouseover”属性,也没有自定义的虚构属性。下面我有两个我不满意的例子。有什么好主意吗?
示例 1:
View.aspx
<td>
<input type="button" value="EDIT" onclick="JSFunction(<%: ObjectId %>)" />
</td>
JavaScript
function JSFunction(id)
{
//some code that does whatever with id
}
示例 2:
View.aspx
<td>
<input type="button" value="EDIT" customAttribute="<%: ObjectId %>" />
</td>
JavaScript
$('input[type=button]').click(function() {
var id = this.attr('customAttribute');
//some code that does whatever with id
});
如果您能想出更好的问题标题,也请分享:)
I render a View with a table in it. Each row of the table is an object that could be edited. So, the last column of this table has a bunch of "EDIT" buttons. When one of these EDIT buttons is clicked, JavaScript function must pick up the Id of the object represented by current row. Ultimately, I would like to end up with a clean HTML: no "onclick", "onmouseover" attributes and no custom made-up attributes. Below I have 2 examples that I'm not thrilled with. Any good ideas?
Example 1:
View.aspx
<td>
<input type="button" value="EDIT" onclick="JSFunction(<%: ObjectId %>)" />
</td>
JavaScript
function JSFunction(id)
{
//some code that does whatever with id
}
Example 2:
View.aspx
<td>
<input type="button" value="EDIT" customAttribute="<%: ObjectId %>" />
</td>
JavaScript
$('input[type=button]').click(function() {
var id = this.attr('customAttribute');
//some code that does whatever with id
});
P.S. If you could come up with a better question title, please share as well :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我过去处理这个问题的一种方法是使用 html5 数据属性。 jQuery 1.4.3 版本和多于。
然后在 jquery 中,您可以执行以下操作:
通过使用 data 属性,您可以拥有一个丰富的 json 对象,它可以包含比属性更多的信息。另外,您只需声明一个数据属性即可保存所有相关信息。
jsfiddle 上的示例。
One way I handled this in the past is to use the html5 data-attribute. Which is picked up by versions of jQuery 1.4.3 and above.
Then in your jquery you can do the following:
By using the data attribute you can have a rich json object that could contain more information than just an attribute. Plus you only have to declare one data-attribute to hold all relevant information.
Example of this working on jsfiddle.
我这样做的方式是让服务器将 id 渲染到
标记,您可以组成自己的属性或将其存储在
id
属性中。然后,如果td
中有一个编辑按钮,您只需编写 jQuery 来查找存储在标记中的 id。
html:
jQuery:
虽然我通常为我的编辑按钮分配一个“编辑”类,而不是按其类型选择它们(因为我在页面上有一个保存按钮)。
The way I do it is I have the server render the id to the
<tr>
tag, you could either make up your own attribute or store it in theid
attribute. Then if you have a edit button inside atd
you just write jQuery to find the id stored in the<tr>
tag.html:
jQuery:
Although I usually assign a class of "edit" to my edit buttons rather than selecting them by their type (as I have a save button on the page).
我会使用 jQuery 元数据插件,因为数据可以通过多种不同的方式嵌入到任何元素上。通常的方法是将其添加到类中,如下所示:
I would use the jQuery metadata plugin as then data can be embedded in a number of different ways onto any element. The usual way is to add it to the class like this: