创建的动态元素未发布
在用户从另一个选择元素做出选择后,我动态创建一个 HTML 选择元素。然而,用于检查表单的 PHP 脚本无法识别新元素并指示它为空。
以下是片段:
some.js:
if(document.getElementById(firstselect).value=='somvalue'){
document.getElementById(gamsDiv).removeChild(gamsElem);
gamsElem = document.createElement("select");
gamsElem.id = "FacilityGamsId";
gamsElem.name = "FacilityGamsId";
gamsElem.setAttribute("onChange", "updateHidden(this);makeUpdateRequest(arguments[0],this)");
opt = document.createElement("option");
opt.text="Select one ";
opt.value=0;
gamsElem.options.add(opt);
.....some other stuff
.....
document.getElementById(gamsDiv).appendChild(gamsElem);
}
PHP 脚本:
if($_POST){
var_dump($_POST['FacilityGamsId'];
}
result: null
服务器 post 操作无法识别 JS 脚本中的任何动态 HTML 元素是否有任何原因。如果我检查/检查新创建的元素,名称和 id 完全相同。
I create an HTML select element dynamically after a user choice is made from another select element. However the PHP script for inspecting the form does not recognise the new element and is indicating it null.
Here is snippets:
some.js:
if(document.getElementById(firstselect).value=='somvalue'){
document.getElementById(gamsDiv).removeChild(gamsElem);
gamsElem = document.createElement("select");
gamsElem.id = "FacilityGamsId";
gamsElem.name = "FacilityGamsId";
gamsElem.setAttribute("onChange", "updateHidden(this);makeUpdateRequest(arguments[0],this)");
opt = document.createElement("option");
opt.text="Select one ";
opt.value=0;
gamsElem.options.add(opt);
.....some other stuff
.....
document.getElementById(gamsDiv).appendChild(gamsElem);
}
The PHP script:
if($_POST){
var_dump($_POST['FacilityGamsId'];
}
result: null
Is there any reason why the server post action does not recognise ANY of my dynamic HTML elements from JS script. If I examine/inspect the newly created elements, the name and id are exactly same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它不会添加到存储在浏览器 DOM 中的表单中。
这个人试图做同样的事情:创建的动态元素未发布
一个解决方案是在服务器端构建动态表单 - 如果它确实需要在客户端,我认为您需要在以下位置操作表单(存储在
document.forms
中):除了 DOM 之外。It's not added to the form stored in the DOM of the browser.
This guy was trying to do the same: dynamic element created not POSTed
A solution would be to build the dynamic form server-side -- if it really needs to be client-side, I think you need to manipulate the form (stored in
document.forms
) in addition to the DOM.这里:
add 是一种方法选择元素界面,而不是 <一个href="http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-30606413" rel="nofollow">选项(这是一个 HTMLElement Collection 并且没有 add< /em> 方法)。所以:
应该解决这个问题。
请注意 gamsElem.add(opt);在 Mozilla firefox 6 中不起作用,所以我恢复到我的旧代码 gamsElem.options.add(opt)。
而且:
会更好:
虽然我不知道参数是什么[ 0] 应参考。
这是通常如何完成的(尽管按钮会调用一个函数而不是一段内联代码,我这样做只是为了方便起见):
需要第二个选项来获取 onchange< /em> 侦听器开火。
如果您想用另一个元素替换一个元素,请创建新元素,然后替换旧元素:
或者您可以只替换选项(将选择的 options.length 设置为零,然后添加新元素)。
Here:
add is a method of the select element interface, not options (which is an HTMLElement Collection and doesn't have an add method). So:
should fix the issue.
Note that gamsElem.add(opt); does not work in Mozilla firefox 6 so I reverted to my old code of gamsElem.options.add(opt).
And:
would be better as:
though I have no idea what arguments[0] should reference.
Here is how this would be done normally (though the button would call a function rather than have a slab of inline code, I've done it that way for convenience only):
A second option is needed to get the onchange listener to fire.
If you want to replace one element with another, create the new element then just replace the old one:
Or you can just replace the options (set the select's options.length to zero and just add the new ones).
您必须将新元素添加到 FORM 标记中
You have to add a new element into your FORM tag
DOM 准备好后运行此代码,最好是 jquery 中的 $.ready() 。
并在 div 名称周围加上引号(“gamsDiv”)
Run this code after the DOM is ready, preferably $.ready() in jquery.
and also put the quotes aroun div name ("gamsDiv")