简体   繁体   English

如何使用jQuery toggle()和隐藏切换链接或按钮?

[英]How To jQuery toggle() and Hide Toggle Link or Button?

I am using PHP sessions to to provide specific forms on a page and need to toggle between a "show form" link and the form. 我正在使用PHP会话在页面上提供特定的表单,并且需要在“显示表单”链接和表单之间进行切换。 I have this working using jQuery toggle(), but I cannot figure out how to prevent the "show form" link from displaying in both toggle states. 我可以使用jQuery toggle()进行此操作,但是我无法弄清楚如何防止“显示表单”链接在两种切换状态下均显示。

I have experimented with jQuery hide() after toggle(), but this made everything disappear, and I have tried using CSS visibility: hidden (which also just caused everything, including the PHP content, to disappear). 我在toggle()之后尝试了jQuery hide(),但这使所有内容都消失了,并且我尝试使用CSS可见性:hidden(这也导致包括PHP内容在内的所有内容都消失了)。

<div id="togLink">
<?php echo $JQclick; ?>
</div>

<div id="showForm">
<?php require_once $_SERVER["DOCUMENT_ROOT"] . '/formInc.php'; ?>
</div>

<script>
function toggleForm() {
  $("#showForm").toggle();
  $("#togLink").toggle();
}

$("#togLink").click(function() {
  toggleForm();
});

$("#showForm").click(function() {
  toggleForm();
});
</script>

The above code works, but the PHP output is displayed in both toggle states (which, I know, is the expected behaviour). 上面的代码有效,但是PHP输出以两种切换状态显示(我知道这是预期的行为)。 As I said above, I need some way of making the toggle state "either or" - clickable link or form, not both. 就像我在上面说的,我需要一种使切换状态为“或”的方式-可点击的链接或形式,而不是两者。 Can anyone offer any suggestions for this? 有人可以为此提供任何建议吗?

... ... ……

CSS code: CSS代码:

@font-face {
    font-family: 'blair_capsregular';
    src: url('../.typefaces/blair_caps-webfont.woff2') format('woff2'),
         url('../typefaces/blair_caps-webfont.woff') format('woff');
    font-weight: normal;
    font-style: normal;
}

#ExBox {
    width: 600px;
    margin: 50px auto 0 auto;
    text-align: justify;
    font-family: verdana, arial, sans-serif;
    font-size: 11px;
    line-height: 1.6;
}

.titleBar {
    width:100%;
}

.Tbox {
    float:left; height:25px;
    text-align: center;
    font-size: 15px;
    font-family: 'blair_capsregular';
}

#box {
    border: 2px solid blue;
    margin: 0;
    position: static;
    padding: 0 2px 0 2px;
    text-decoration: none;
}

#box a:link {
    color: orange;
    text-decoration: none;
}

#box a:visited {
    color: orange;
    text-decoration: none;
}

.Tbox:nth-child(1) {
    width:33.3%;
}

.Tbox:nth-child(2) {
    width:33.3%;
}

.Tbox:nth-child(3) {
    width:33.3%;
}

.clearRed {
    clear: both;
    color: red;
}

.Tbox a:link {
    color: black;
    text-decoration: none;
}

.Tbox a:visited {
    color: black;
    text-decoration: none;
}

.Tbox a:hover {
    color: red;
    text-decoration: none;
}

.Tbox a:active {
    color: hotpink;
    text-decoration: none;
}

.Tbox a:focus {
    color: hotpink;
    text-decoration: none;
}





/* FORM CSS - Placeholder colors */
::-webkit-input-placeholder { /* WebKit, Blink, Edge */
    color:    #9b9b9b;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
   color:    #9b9b9b;
   opacity:  1;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
   color:    #9b9b9b;
   opacity:  1;
}
:-ms-input-placeholder { /* Internet Explorer 10-11 */
   color:    #9b9b9b;
}
::-ms-input-placeholder { /* Microsoft Edge */
   color:    #9b9b9b;
}

textarea::placeholder { color: #9b9b9b; font-family: arial; }

/* Colors for focused fields */

input[type=text], input[type=email], textarea {
  outline: none;
  border: 1px solid #9b9b9b;
}

input[type=text]:focus, input[type=email]:focus, textarea:focus {
  border: 1px solid #00C5BE;
}

/* Input styling */

textarea {
   font-family: arial;
   width: 27rem;
   font-size: 1rem;
   padding: 0.6rem;
   margin-right: 0.5rem;
   margin-left: 0.5rem;
   border-radius: 5px;
   border: 1px solid #9b9b9b;
   color: #9b9b9b !important;
}

.contact-form-div input {
   display: block;
   font-size: 1rem;
   width: 27rem;
   padding: 0.6rem;
   margin: 0.5rem;
   border-radius: 5px;
   border: 1px solid #9b9b9b;
   color: #9b9b9b !important;
}

.contact-form-div input[type=submit] {
   width: auto;
   background-color: #00C5BE;
   border: none;
   color: #fff !important;
   font-size: 1em;
   padding: 10px 50px;
   text-transform: uppercase;
   font-weight: normal;
}

/* Hide the fake field */
#m66 {
   display: none;
}


/* VERTICAL SLIDER */


* { margin:0; padding:0; }
a { text-decoration: none; }  

.expand {
  background: #fff;
  overflow: hidden;
  color: #000;   
  line-height: 50px;

  transition: all .5s ease-in-out;
  height: 0;
 }

 .expand:target {
  height: 50px;
}

.close {
    max-height: 0;
}

/* JQUERY TESTING */

.box{
    display:none;
}

#showForm {
  display: none;
}

... ... ……

Thanks to Grant Noe this is almost working. 感谢Grant Noe,这几乎可以正常工作。 With the above code everything works perfectly ...except that clicking on the form causes it to disappear. 使用上面的代码,一切都可以正常工作...除了单击表单导致其消失之外。 Grant has since revised the code (again, thanks); 此后,格兰特修改了代码(再次感谢); but the revised code, whilst addressing the problem of the disappearing form, has a visible " contact form " link in both toggle states and loads the form first, not the link. 但是修改后的代码在解决表单消失的问题的同时,在两个切换状态下都有一个可见的“ 联系表单 ”链接,并首先加载该表单,而不是链接。 The latter should be simple to fix; 后者应该易于修复; but I cannot figure out how to show either the contact form link or the form, not both in both toggle states. 但我无法弄清楚如何显示联系表单链接表单,但不能同时显示两种切换状态。

The code, when using Grant's revised code, is as follows: 使用Grant的修订代码时,代码如下:

<div id="togLink">
<?php echo $JQclick; ?>
</div>

<div id="showForm">
<?php require_once $_SERVER["DOCUMENT_ROOT"] . '/form.php'; ?>
</div>

<script>
$("#togLink").click(function() {
  $("#showForm").toggle();
  $("#contactForm").toggle();
});
</script>


@font-face {
    font-family: 'blair_capsregular';
    src: url('../.typefaces/blair_caps-webfont.woff2') format('woff2'),
         url('../typefaces/blair_caps-webfont.woff') format('woff');
    font-weight: normal;
    font-style: normal;
}

#ExBox {
    width: 600px;
    margin: 50px auto 0 auto;
    text-align: justify;
    font-family: verdana, arial, sans-serif;
    font-size: 11px;
    line-height: 1.6;
}

.titleBar {
    width:100%;
}

.Tbox {
    float:left; height:25px;
    text-align: center;
    font-size: 15px;
    font-family: 'blair_capsregular';
}

#box {
    border: 2px solid blue;
    margin: 0;
    position: static;
    padding: 0 2px 0 2px;
    text-decoration: none;
}

#box a:link {
    color: orange;
    text-decoration: none;
}

#box a:visited {
    color: orange;
    text-decoration: none;
}

.Tbox:nth-child(1) {
    width:33.3%;
}

.Tbox:nth-child(2) {
    width:33.3%;
}

.Tbox:nth-child(3) {
    width:33.3%;
}

.clearRed {
    clear: both;
    color: red;
}

.Tbox a:link {
    color: black;
    text-decoration: none;
}

.Tbox a:visited {
    color: black;
    text-decoration: none;
}

.Tbox a:hover {
    color: red;
    text-decoration: none;
}

.Tbox a:active {
    color: hotpink;
    text-decoration: none;
}

.Tbox a:focus {
    color: hotpink;
    text-decoration: none;
}





/* FORM CSS - Placeholder colors */
::-webkit-input-placeholder { /* WebKit, Blink, Edge */
    color:    #9b9b9b;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
   color:    #9b9b9b;
   opacity:  1;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
   color:    #9b9b9b;
   opacity:  1;
}
:-ms-input-placeholder { /* Internet Explorer 10-11 */
   color:    #9b9b9b;
}
::-ms-input-placeholder { /* Microsoft Edge */
   color:    #9b9b9b;
}

textarea::placeholder { color: #9b9b9b; font-family: arial; }

/* Colors for focused fields */

input[type=text], input[type=email], textarea {
  outline: none;
  border: 1px solid #9b9b9b;
}

input[type=text]:focus, input[type=email]:focus, textarea:focus {
  border: 1px solid #00C5BE;
}

/* Input styling */

textarea {
   font-family: arial;
   width: 27rem;
   font-size: 1rem;
   padding: 0.6rem;
   margin-right: 0.5rem;
   margin-left: 0.5rem;
   border-radius: 5px;
   border: 1px solid #9b9b9b;
   color: #9b9b9b !important;
}

.contact-form-div input {
   display: block;
   font-size: 1rem;
   width: 27rem;
   padding: 0.6rem;
   margin: 0.5rem;
   border-radius: 5px;
   border: 1px solid #9b9b9b;
   color: #9b9b9b !important;
}

.contact-form-div input[type=submit] {
   width: auto;
   background-color: #00C5BE;
   border: none;
   color: #fff !important;
   font-size: 1em;
   padding: 10px 50px;
   text-transform: uppercase;
   font-weight: normal;
}

/* Hide the fake field */
#m66 {
   display: none;
}


/* VERTICAL SLIDER */


* { margin:0; padding:0; }
a { text-decoration: none; }  

.expand {
  background: #fff;
  overflow: hidden;
  color: #000;   
  line-height: 50px;

  transition: all .5s ease-in-out;
  height: 0;
 }

 .expand:target {
  height: 50px;
}

.close {
    max-height: 0;
}

/* JQUERY TESTING */

.box{
    display:none;
}

#togLink {
  color: blue;
  cursor: pointer;
}

#togLink:hover {
  text-decoration: underline;
}

#showForm,
#contactForm {
  width: 425px;
  height: 550px;
}

#showForm {
  background-color: #DDD;
}

#contactForm {
  background-color: #AAA;
  display: none;
}

I have not added-in contactForm to the HTML side of things, because it does not fix the persistent " contact form " link and leaves an ugly background-colour box behind even when hiding the form but failing to hide the link. 我没有在HTML方面添加contactForm,因为它不能修复持久的“ contact form ”链接,即使隐藏表单但无法隐藏链接也不会留下难看的背景色框。

... ... ……

26 May 2019: with reference to a reply by rg88 [ How do I hide a part of a form and make it visible only on clicking a "Add another" button? 2019年5月26日:参考rg88的回复[ 如何隐藏表单的一部分并使之仅在单击“添加其他”按钮时可见? ] this is what finally worked: ] 是最终有效的方法:

<a id="togLink" href="#"><?php echo $formClick; ?></a>
<div id="togForm"><?php require_once $_SERVER["DOCUMENT_ROOT"] . '/form.php'; ?>
</div>

<script>
$( "#togLink" ).on( "click", function() {
  $('#togForm').toggle();
});

</script>
#togForm {
  display: none;
}

There is still the issue of not being able to make the words " Contact Form " disappear when the form is loaded or being able to replace the words with something else, but it appears that it is not possible to make this happen because the Contact Form link (in this case) is the toggle point and therefore has to remain constant in all toggle states. 仍然存在无法在加载表单时使“ 联系表单 ”一词消失或能够用其他东西替换这些单词的问题,但是似乎无法实现此操作,因为联系表单link(在这种情况下)是切换点,因此必须在所有切换状态下保持恒定。

You're toggling just the one element right now, given this format: $(toggled-element).toggle(); 给定以下格式,您现在只切换一个元素: $(toggled-element).toggle(); . You'll need to toggle both elements like this: 您需要像这样切换两个元素:

$('#togLink').click(function(){
  $('#showForm').toggle();
  $('#contactForm').toggle();
});

If you initially want the link showing and the form hidden, you could hide the form with css with display: none; 如果最初希望显示链接并隐藏表单,则可以使用display: none;为css的表单来隐藏表单display: none; , or at the top of your html in a script with $('#showForm').hide(); ,或使用$('#showForm').hide();在脚本中的html顶部$('#showForm').hide(); . So the total result would be something like this: 因此,总结果将如下所示:

 $("#togLink").click(function() { $("#showForm").toggle(); $("#contactForm").toggle(); }); 
 #togLink { color: blue; cursor: pointer; } #togLink:hover { text-decoration: underline; } #showForm, #contactForm { width: 425px; height: 550px; } #showForm { background-color: #DDD; } #contactForm { background-color: #AAA; display: none; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="togLink">Toggle Form</div> <div id="showForm"></div> <div id="contactForm"></div> 

Let me know if I missed something. 让我知道我是否错过了什么。

Changelog: 变更日志:

I added the #contactForm div that's not seen in OP to adjust to OP use-case requests. 我添加了OP中未显示的#contactForm div以适应OP用例请求。 The clarification occurred in the comments on my answer. 在对我的答案的评论中进行了澄清。

Placing the PHP in an HTML element allows you to have separate control over that element: 将PHP放在HTML元素中可以使您对该元素进行单独控制:

<div id="togLink">foo
<?php echo $JQclick; ?>
</div>

<div id="showForm">bar
<span>glorp<?php require_once $_SERVER["DOCUMENT_ROOT"] . '/formInc.php'; ?></span>
</div>

Use CSS to hide the element: 使用CSS隐藏元素:

span {
  display: none;
}

Then avoid the element to be toggled, using .not() : 然后,使用.not()避免切换元素:

$('#togLink').click(function(){
  console.log('click');
  $('#showForm').not('span').toggle();
});

Here is a DEMO 这是一个演示

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

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