简体   繁体   English

可以在JavaScript中将变量作为元素ID传递吗

[英]Can a variable be passed as an element id in javascript

I'm trying to get some AJAX running with Django using jQuery. 我正在尝试使用jQuery在Django中运行一些AJAX。 The problem is that the ID of the element I'm trying to change isn't set in stone, it's a Django template variable so it is different for each element in the table, but I want to use AJAX to change that element. 问题是我要更改的元素的ID并不是一成不变的,它是Django模板变量,因此表中的每个元素都不同,但是我想使用AJAX来更改该元素。 I'm passing the dictionary from a Django view to JavaScript via simplejson, the pertinent objects being ID and model (not real variable names, just wanted to make it obvious what I am trying to do). 我正在通过simplejson将字典从Django视图传递到JavaScript,相关的对象是ID和模型(不是真实的变量名,只是想让它变得很明显我正在尝试做)。 Some pseudo code (third line, wrote it like python because I don't know how to do this in JavaScript): 一些伪代码(第三行,就像python一样写,因为我不知道如何在JavaScript中执行此操作):

var quantity_update = function(msg, elem) {
    var response_obj = eval('(' + msg + ')');
    var newtotal = document.getElementById("%s, %s"); % (response_obj.id, response_obj.model)
    subtotal.innerHTML = "<td>"+response_obj.subtotal+"</td>"; 

Is something like this possible, and if so how? 这样的事情有可能吗?

If your element ID is not set in stone, you might want to identify the element using other means. 如果您的元素ID并非一成不变,则可能需要使用其他方式来标识该元素。 Many JS framework such as jQuery supports identification via CSS Selector. 许多JS框架(例如jQuery)都支持通过CSS Selector进行标识。 For example you can identify the element you want to change by combination of its tag and class. 例如,您可以通过标签和类的组合来标识要更改的元素。

For documentation on the available selector you should read jQuery Selector Documentation 有关可用选择器的文档,请阅读jQuery选择器文档。

Javascript doesn't have fancy string formatting (%s), but you can use plain string concatenation, just like you can in Python: Javascript没有花哨的字符串格式(%s),但是您可以使用纯字符串连接,就像在Python中一样:

var id = response_obj.id + ', ' + resonse_obj.model

You don't need to worry about the string being generated dinamically. 您无需担心字符串是动态生成的。 It is still a string after all and getElementById accepts it the same way . 毕竟它仍然是一个字符串,并且getElementById以相同的方式接受它。


You probably shouldn't be doing the eval() yourself to parse the JSON. 您可能不应该自己执行eval()来解析JSON。 I'm pretty sure you can have JQuery do that for you when you do the AJAX request. 我很确定当您执行AJAX请求时,您可以让JQuery为您执行此操作。

You can totally pass jquery css selectors as variables. 您可以完全将jquery css选择器作为变量传递。 Simple example: 简单的例子:

var elementId = $('td').eq(0).attr('id'),
    myElement = $('#'+elementId); 

In this example, we grab the ID of the first TD in your document, find its id, and use that to get "myElement" 在此示例中,我们获取文档中第一个TD的ID,找到其ID,然后使用该ID来获取“ myElement”

You may find it helpful to review the "selectors" section of the jQuery documentation (api.jquery.com) to review all your options for finding an element. 您可能会发现查看jQuery文档(api.jquery.com)的“选择器”部分以查看所有查找元素的选项很有帮助。 In my example I also am using the jquery.eq() method, which filters a list of selectors (in this case all the TDs) to a specific one. 在我的示例中,我还使用了jquery.eq()方法,该方法将选择器列表(在本例中为所有TD)筛选为一个特定的选择器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM