javascript 函数中 HTML 表单标记内的 Javascript

发布于 2024-12-18 13:02:55 字数 1134 浏览 0 评论 0原文

是否可以将 JavaScript 代码放入 HTML 表单标记中。我试图实现这样的目标:

function abc(abcd) {
    document.getElementById("context").innerHTML += "<br> 1:";
    document.getElementById("context").innerHTML += "***<form METHOD=POST ACTION=\"/InformationRetrieval/home\">***<input type=\"checkbox\" name=\" GoogleRelevance\" value=\"1\"> Relevant <br><br>";

    document.getElementById("context").innerHTML += "<br> 2:";
    document.getElementById("context").innerHTML += "<input type=\"checkbox\" name=\" GoogleRelevance1\" value=\"1\"> Relevant <br><br>";

    document.getElementById("context").innerHTML += "<br> 3:";
    document.getElementById("context").innerHTML += "<input type=\"checkbox\" name=\" GoogleRelevance3\" value=\"1\"> Relevant <br>***<input type=\"submit\" value=\"Submit\"></form>***<br><br>";
}

在这个例子中我想问的是: 我最初在开始的 2-3 行中启动了标签,但我没有在那里关闭它,而是插入了更多 javascript 行,最后我用另一个 ,但是当我单击此按钮时它不起作用。

我也尝试使用 OnClick 属性,但这也不起作用。

我被它困住了。任何帮助将不胜感激。

Is it possible to put a JavaScript code within the HTML Form tags. I am trying to achieve something like this:

function abc(abcd) {
    document.getElementById("context").innerHTML += "<br> 1:";
    document.getElementById("context").innerHTML += "***<form METHOD=POST ACTION=\"/InformationRetrieval/home\">***<input type=\"checkbox\" name=\" GoogleRelevance\" value=\"1\"> Relevant <br><br>";

    document.getElementById("context").innerHTML += "<br> 2:";
    document.getElementById("context").innerHTML += "<input type=\"checkbox\" name=\" GoogleRelevance1\" value=\"1\"> Relevant <br><br>";

    document.getElementById("context").innerHTML += "<br> 3:";
    document.getElementById("context").innerHTML += "<input type=\"checkbox\" name=\" GoogleRelevance3\" value=\"1\"> Relevant <br>***<input type=\"submit\" value=\"Submit\"></form>***<br><br>";
}

Here what I am trying to ask in this example is:
I have started the tag initially in my starting 2-3 lines, but I am not closing it there and rather inserting some more javascript lines and in the end I am closing the tag with another <input type="submit">, but when I click this button it does not work.

I tried using the OnClick property too but that didn't work either.

I am badly stuck with it. Any help will be greatly appreciated.

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

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

发布评论

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

评论(1

剑心龙吟 2024-12-25 13:02:55

我认为你真的应该使用一些好的ajax库(jQuery,YUI,dojo,goog,...)来做这样的事情,将所有代码填充到表单属性中感觉不对。正如 nickf 指出的那样,以这种方式修改 dom 属性确实不是一个好主意。由于我习惯了 jQuery,这里有一个片段,我从您的帖子中了解到问题所在:

// Caching the stuff that you used to += on the innerHTML
var newStuff = ' /* your form code here */ '; 

jQuery
.ajax({
    url: /* your request to google */,
    context: jQuery('#context') // Caching the context dom node
})
.done(function() { 
    // The request was successful
    jQuery(this) // The cached context node is the context of this callback
    .append(newStuff); // You only have to append new stuff here, if you want something
                       // more dynamic insert the logic in this function
})
.fail(function() { /* alert("error"); */ })
.always(function() { /* alert("complete"); */ });

不过我没有测试这个,这只是一个建议。对我来说,它更干净,并且如果您让它在一种浏览器中工作,那么它的优点是您可以依靠 jQuery 来确保它也可以在大多数其他浏览器中工作。

顺便说一句:你可以在整个 html 字符串周围使用单个 ' ,这样你就不需要转义 " 东西。

希望这有帮助,祝你好运!

I think you really should use some good ajax library (jQuery, YUI, dojo, goog, ...) for something like this, stuffing all the code into form attributes doesn't feel right. As nickf pointed out modifying a dom property in this way is really not a good idea. As I'm used to jQuery here is a snippet to what I understand from your post is the problem:

// Caching the stuff that you used to += on the innerHTML
var newStuff = ' /* your form code here */ '; 

jQuery
.ajax({
    url: /* your request to google */,
    context: jQuery('#context') // Caching the context dom node
})
.done(function() { 
    // The request was successful
    jQuery(this) // The cached context node is the context of this callback
    .append(newStuff); // You only have to append new stuff here, if you want something
                       // more dynamic insert the logic in this function
})
.fail(function() { /* alert("error"); */ })
.always(function() { /* alert("complete"); */ });

I didn't test this one though, it's only a recommendation. To me it is cleaner and it has the advantage if you get it to work in one browser this way you can count on jQuery to make sure it's working in most other browsers too.

And by the way: you can use single ' around the whole html string so that you don't need to escape the " thingys.

Hope this helps, good luck!

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