简体   繁体   English

使用javascript / php创建元素,需要获取其唯一ID并将其发送回PHP

[英]Creating elements with javascript/php, need to get their unique ID and send it back to PHP

I have a script that sends a POST to a php file, and gets a new integer (primary key). 我有一个脚本,该脚本将POST发送到php文件,并获取一个新的整数(主键)。 Then the script creates 3 new divs, with that ID included in them as an attribute. 然后,脚本会创建3个新的div,其中包含该ID作为属性。

Now, I need to be able to grab the ID and give it back to PHP when the user updates it. 现在,我需要能够获取ID并在用户更新ID时将其返回给PHP。 (It's currently a div, but later it will be an editable form.) (当前为div,但稍后它将是可编辑的表单。)

I was going to do this by creating a button in the same script that I use to create the divs, bearing the same integer and calling a function. 为此,我将在用于创建div的同一脚本中创建一个按钮,并使用相同的整数并调用一个函数。 The problem with this concept, is that I have no idea how to create a Unique variable to pass along to the function, so it knows which number to look at. 这个概念的问题在于,我不知道如何创建一个唯一变量传递给函数,因此它知道要看哪个数字。 Obviously, if I have only one variable that gets overwritten with each new entry - after all of the rows load, only the last row will have a variable that matches its id. 显然,如果我只有一个变量被每个新条目覆盖-在加载所有行之后,只有最后一行将具有与其ID相匹配的变量。

Another option is to find the value of all attributes that bear the name data-integer-question and pass its number/integer to a function, when the user does something like keyup enter. 另一种选择是,当用户执行键入keyup之类的操作时,查找带有名称data-integer-question的所有属性的值,并将其数字/整数传递给函数。 However, I have no idea how to do that. 但是,我不知道该怎么做。

obj.returnIntJson holds the integer that I've been talking about, which changes for each row. obj.returnIntJson保存着我一直在谈论的整数,每行都会改变。

rowNumberObj.qarowcontainer is just a reference for the parent div, which doesn't get edited by the user - you can ignore it. rowNumberObj.qarowcontainer只是父div的引用,用户不会对其进行编辑-您可以忽略它。

The script gives a result like this: <div id="questioncolumn" class="questionColumnCSS" data-integer-question="701"></div> 该脚本给出如下结果: <div id="questioncolumn" class="questionColumnCSS" data-integer-question="701"></div>

Suggestions on how to proceed? 有关如何进行的建议? Here's the code for one of the divs, 这是其中一个div的代码,

Thanks. 谢谢。

    qaRowQuestion = document.createElement('div');
    qaRowQuestion.setAttribute('id', "questioncolumn");
    qaRowQuestion.setAttribute('data-integer-question', obj.returnIntJson);
    qaRowQuestion.setAttribute('class',"questionColumnCSS");

    qaRowQuestionParent = document.getElementById(rowNumberObj.qarowcontainer);
    qaRowQuestionParent.appendChild(qaRowQuestion);

This script worked: 该脚本有效:

$(document).on("click", ".questionColumnCSS",function() {
  var questionUpdate = ($(this).data('integer-question'));
});

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

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