jQuery 无法访问表单附加元素
有问题...我正在使用 jQuery 将表单元素附加到页面。但是我无法使用 js 访问表单元素。我认为这是因为 dom 已加载并且表单元素在 dom 中不可用。有解决办法吗?我知道我可以附加一个 Iframe,其中包含表单,但我不愿意。
这是一个例子...
<body>
<div id=wrapper>
</div>
<div onlclick='newInput()'>
Add Detail...
</div>
</body>
还有 jQuery...
function newInput(){
var container = "<div id='input-container'><input id='input' type='text' value='insert new data here'></input><div id='submit onclick='submitInput()'>SUBMIT</div>";
$('div#wrapper').append(container);
}
function submitInput(){
var input = document.getElementById('input').value;
....do stuff with ne input here....
}
Have a problem... I am appending a form element to a page with jQuery. However i cannot access the form element with js. I think this is because the dom has loaded and the form element isn't available in the dom. Is there a work around for this. I know I can append an Iframe with the form inside but would rather not.
Heres an example...
<body>
<div id=wrapper>
</div>
<div onlclick='newInput()'>
Add Detail...
</div>
</body>
And the jQuery...
function newInput(){
var container = "<div id='input-container'><input id='input' type='text' value='insert new data here'></input><div id='submit onclick='submitInput()'>SUBMIT</div>";
$('div#wrapper').append(container);
}
function submitInput(){
var input = document.getElementById('input').value;
....do stuff with ne input here....
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么下面显示两个单引号?
我会仔细检查并确保控制台中没有出现任何错误,以防 js 抛出错误。你的代码对我有用 http://jsfiddle.net/4xxGJ/
Why do you have two single quotes shown below ?
I would double check and make sure you're not getting any errors in the console, just in case the js is throwing an error. Your code works for me http://jsfiddle.net/4xxGJ/