Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将JavaScript变量分配给HTML标记?_Javascript_Html_Variables_Assign - Fatal编程技术网

如何将JavaScript变量分配给HTML标记?

如何将JavaScript变量分配给HTML标记?,javascript,html,variables,assign,Javascript,Html,Variables,Assign,我想将我的id变量作为id分配给我的select标记,如下所示。我怎样才能正确地做呢 var id = 0; $('#section2').append("<div><select id='" + id + "' class='form-control' name='rendezo_neve[]' style='max-width:90%'>" + $.get("php/getElements.php", function(data) { $('#' + id).ht

我想将我的id变量作为id分配给我的select标记,如下所示。我怎样才能正确地做呢

var id = 0;
$('#section2').append("<div><select id='" + id + "' class='form-control' name='rendezo_neve[]' style='max-width:90%'>" + $.get("php/getElements.php", function(data) {
  $('#' + id).html(data)
}) + "</select><input type='button' value='X' class='remove_field' style='float:right;position:relative;top:-34px;right:-12px;font-size:12px;height:33px;width:33px;font-weight:bold;color:red;background-color:white;border:1px solid lightgrey;border-radius:50%'></input></div>");
var id=0;
$('#section2').append(“+$.get”(“php/getElements.php”),函数(数据){
$('#'+id).html(数据)
}) + "");

您可以使用此代码
$('select[name='+name+']).attr('id',id)
。首先,使用name选择select下拉列表,然后设置ID属性及其值。

为什么在一行中有半个程序?使用带有小步的单独命令,将中间结果放入变量中,您会发现调试更容易,因为您可以仔细检查正在发生的事情。$.get是一个异步命令,它返回一个请求对象,而不是请求的结果字符串。