简体   繁体   English

如何在单击 div 时显示和关闭表单?

[英]How to show and close form on click of a div?

When a div is clicked I want to show a form, as done on this page .单击 div 时,我想显示一个表单,如本页中所做的那样。 This is what I have tried ( fiddle ):这是我尝试过的(小提琴):

$(document).on("click","#tawkchat-minified-container",function() {
    var htmldynamic = '  <form id="test" action="test.php">\
<div>\
  Test: <input name="blah" value="test" type="text">\
</div>\
</form>'
    $("#maximizeChat").html(htmldynamic);
});

I don't know if this is the right way to do it.我不知道这是否是正确的方法。 Is there a better approach?有没有更好的方法?

Adding large chunks of HTML as JavaScript variables is not good practice.将大块 HTML 添加为 JavaScript 变量并不是一个好习惯。 It is easy to make errors in the HTML as you have to read it awkwardly embedded in the JS.很容易在 HTML 中出错,因为您必须笨拙地阅读嵌入在 JS 中的 HTML。

A better approach is to include the HTML code with the rest of your markup, but use CSS to hide it.更好的方法是将 HTML 代码包含在标记的其余部分中,但使用 CSS 将其隐藏。 Then you can just show it using JavaScript when it is pressed.然后你可以在按下它时使用 JavaScript 显示它。

HTML: HTML:

<div id="my-form-container">
  <div id="my-form-header">My form</div>
  <form id="my-form" action="test.php">
    <div>
      Test: <input name="blah" value="test" type="text">
    </div>
  </form>
</div>

CSS: CSS:

#my-form { 
  display: none; /* This will hide the form. */
}

JavaScript: JavaScript:

//When the container is clicked...
$("#my-form-container").click(function() {
  //...show the form.
  $("#my-form").show();
});

Use this approach will definitely solve your problem使用这种方法肯定能解决你的问题

 $(document).ready(function(){ $("#tawkchat-minified-agent-container").click(function() var hlink = $("#test").val(); $("#test").click(function(){ $(".form").show() }); }); });

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

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