使用 jquery 将表单插入页面?

发布于 2024-12-02 18:29:48 字数 964 浏览 0 评论 0原文

我正在使用 jquery 将隐藏表单插入到页面中(一旦页面已经加载)。由于某种原因,该代码无法正常工作。以下是代码示例:

$(function(){
    $('body')
        .append('<form id="mySearch"></form>'); //append a new form element with id mySearch to <body>
    $('#mySearch') 
        .attr("action","mySearchPage.cfm") .attr("method","post") //set the form attributes
        //add in all the needed input elements
        .append('<input type="hidden" name="btnSearch" id="btnSearch" value="Search">')
        .append('<input type="hidden" name="txtField1" id="txtField1" value="">')
        .append('<input type="hidden" name="txtField2" id="txtField2" value="">')
        .append('<input type="hidden" name="selType" id="selType" value="0">')
        .append('<input type="hidden" name="selType2" id="selType2" value="2">')
});

表单未插入到页面正文中。我做错了什么?

更新:我发现当我在自己的页面中使用代码时,它可以正常工作。当我尝试将其集成到一个大型的现有页面中时,它不再起作用。关于什么可能导致完全相同的代码失败的任何想法?

I'm using jquery to insert a hidden form into a page (once the page has already loaded). For some reason the code is not working. Here is a sample of the code:

$(function(){
    $('body')
        .append('<form id="mySearch"></form>'); //append a new form element with id mySearch to <body>
    $('#mySearch') 
        .attr("action","mySearchPage.cfm") .attr("method","post") //set the form attributes
        //add in all the needed input elements
        .append('<input type="hidden" name="btnSearch" id="btnSearch" value="Search">')
        .append('<input type="hidden" name="txtField1" id="txtField1" value="">')
        .append('<input type="hidden" name="txtField2" id="txtField2" value="">')
        .append('<input type="hidden" name="selType" id="selType" value="0">')
        .append('<input type="hidden" name="selType2" id="selType2" value="2">')
});

The form is not being inserted into the body of the page. What am I doing wrong?

Update: I've found that when I use the code in a page on it's own it works correctly. When I try to integrate it into a large, already existing page it no longer works. Any ideas about what might cause the exact same code to fail?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千寻… 2024-12-09 18:29:48

试试这个:

$(document).ready(function(){
    $('body')
        .append('<form id="mySearch"></form>'); //append a new form element with id mySearch to <body>
    $('#mySearch') 
        .attr("action","mySearchPage.cfm") .attr("method","post") //set the form attributes
        //add in all the needed input elements
        .append('<input type="hidden" name="btnSearch" id="btnSearch" value="Search">')
        .append('<input type="hidden" name="txtField1" id="txtField1" value="">')
        .append('<input type="hidden" name="txtField2" id="txtField2" value="">')
        .append('<input type="hidden" name="selType" id="selType" value="0">')
        .append('<input type="hidden" name="selType2" id="selType2" value="2">')
});

无论如何,你绝对应该避免这种向 dom 添加动态内容的方法。
最好的技术是创建一个包含所有必要标记的单个字符串,并将其一次性注入!像这样的东西:

var HTML = '<form attributes><input><input>...</form>';
$("#container").html(HTML);

try this:

$(document).ready(function(){
    $('body')
        .append('<form id="mySearch"></form>'); //append a new form element with id mySearch to <body>
    $('#mySearch') 
        .attr("action","mySearchPage.cfm") .attr("method","post") //set the form attributes
        //add in all the needed input elements
        .append('<input type="hidden" name="btnSearch" id="btnSearch" value="Search">')
        .append('<input type="hidden" name="txtField1" id="txtField1" value="">')
        .append('<input type="hidden" name="txtField2" id="txtField2" value="">')
        .append('<input type="hidden" name="selType" id="selType" value="0">')
        .append('<input type="hidden" name="selType2" id="selType2" value="2">')
});

Anyway you should absolutely avoid this kind of approach to add dynamic content to the dom.
The best technique is to create a single string containing all the necessary markup and inject it in one shot! Something like:

var HTML = '<form attributes><input><input>...</form>';
$("#container").html(HTML);
森林散布 2024-12-09 18:29:48

它对我有用,也许您不想要隐藏的表单字段?

http://jsfiddle.net/M5vWR/

查看此带有可见表单元素的内容。

http://jsfiddle.net/M5vWR/2/

It works for me, perhaps you don't want hidden form fields?

http://jsfiddle.net/M5vWR/

See this one with visible form elements.

http://jsfiddle.net/M5vWR/2/

离鸿 2024-12-09 18:29:48

毕竟,这只是文件中包含 javascript 的额外标记的语法问题:P 代码现在可以运行了!

After all, it was just a syntax problem with an extra tag in the file that included the javascript :P The code works now!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文