jQuery隐藏和显示文本区域

huangapple go评论67阅读模式
英文:

jQuery to hide and show textarea

问题

我有一个文本框,我想在单击按钮时显示/隐藏它,而且我需要以这种方式定位它,因为我将在未定义的次数中使用它。

SAMPLE IMAGE HERE

SAMPLE IMAGE HERE

这是我想要的HTML

<div class="input-group mb-3">
    <input type="text" name="tname[]" class="form-control" required placeholder="req. for">
    <button class="btn btn-outline-secondary buttonMultipleNameReq" type="button" id="button-addon2"><i class="bi bi-plus-circle"></i></button>
</div>
<textarea class="form-control" id="multipleNameReq"></textarea>

这是我尝试使用的脚本来隐藏/显示它

$('body').on('click', '.buttonMultipleNameReq', function () {
    const element1 = $(this);
    const target1 = element1.parent().parent().find("#multipleNameReq");
    console.log(target1);

    if (target1.style.display === "none") {
        target1.style.display = "block";
    } else {
        target1.style.display = "none";
    }
})
英文:

I have a textarea that I want to show/hide when ever I click a button
and also I need it to target this way because ill be using it in an undefined number of times

SAMPLE IMAGE HERE

SAMPLE IMAGE HERE

> here is the HTML that I want to

&lt;div class=&quot;input-group mb-3&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;tname[]&quot; class=&quot;form-control&quot; required placeholder=&quot;req. for&quot;&gt;
    &lt;button class=&quot;btn btn-outline-secondary buttonMultipleNameReq&quot; type=&quot;button&quot; id=&quot;button-addon2&quot;&gt;&lt;i class=&quot;bi bi-plus-circle&quot;&gt;&lt;/i&gt;&lt;/button&gt;
&lt;/div&gt;
&lt;textarea class=&quot;form-control&quot; id=&quot;multipleNameReq&quot;&gt;&lt;/textarea&gt;
		        

and here is the script I try to use hiding/Showing it

$(&#39;body&#39;).on(&#39;click&#39;, &#39;.buttonMultipleNameReq&#39;,function(){
    const element1 = $(this);
    const target1 = element1.parent().parent().find(&quot;#multipleNameReq&quot;);
    console.log(target1);

    if (target1.style.display === &quot;none&quot;) {
	target1.style.display = &quot;block&quot;;
    } else {
	target1.style.display = &quot;none&quot;;
    }
})

答案1

得分: 1

已接受的答案对于非重复的textarea或其他元素id将有效,但正如您在图像中所示,如果您想要为多个textarea或任何元素工作,我建议您使用class属性而不是id属性在这里有关为什么不要多次使用相同的id的解释,

重复的ID是常见的验证错误,可能会破坏标签的可访问性,例如表单字段和表头单元格。

为了解决问题,如果一个ID值多次使用,请确保每个ID都是唯一的。

下面是一些可能帮助您找到所需内容的代码片段

$(&#39;body&#39;).on(&#39;click&#39;, &#39;.buttonMultipleNameReq&#39;,function(e){
    let textArea = $(e.target).parent().parent().find(&#39;textarea&#39;);
    if(textArea.css(&#39;display&#39;) == &quot;block&quot;){
        textArea.hide()
    }else{
        textArea.show()
    }
})
.multipleNameReq{
  display:block
}
<div>
  <div class=&quot;input-group mb-3&quot;>
      <input type=&quot;text&quot; name=&quot;tname[]&quot; class=&quot;form-control&quot; required placeholder=&quot;req. for&quot;>
      <button class=&quot;btn btn-outline-secondary buttonMultipleNameReq&quot; type=&quot;button&quot; id=&quot;button-addon2&quot;>hide/show</button>
  </div>
  <textarea class=&quot;form-control multipleNameReq&quot;></textarea>
</div>
<div>
<div class=&quot;input-group mb-3&quot;>
    <input type=&quot;text&quot; name=&quot;tname[]&quot; class=&quot;form-control&quot; required placeholder=&quot;req. for&quot;>
    <button class=&quot;btn btn-outline-secondary buttonMultipleNameReq&quot; type=&quot;button&quot; id=&quot;button-addon2&quot;>hide/show</button>
</div>
<textarea class=&quot;form-control multipleNameReq&quot;></textarea>

</div>
<script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js&quot;></script>

使用切换功能

下面是使用jQuery切换的更简单的代码,在这里您可以阅读有关toggle的信息

$(&#39;body&#39;).on(&#39;click&#39;, &#39;.buttonMultipleNameReq&#39;,function(e){
	$(e.target).parent().parent().find(&#39;textarea&#39;).toggle();
})
<script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js&quot;></script>
英文:

the accepted answer will work fine for nonduplicate id of textarea or any other elements, but as you have shown in your image if you want to work this for multiple textarea or any elements I suggest you use the class attribute instead of the id attribute, here is the explanation for why to not use same id multiple times,

> Duplicate IDs are common validation errors that may break the accessibility of labels, e.g., form fields, and table header cells.

> To fix the problem, change an ID value if it is used more than once to be sure each is unique.

here below is some piece of code that may help you to get what are you looking for

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;body&#39;).on(&#39;click&#39;, &#39;.buttonMultipleNameReq&#39;,function(e){
    let textArea = $(e.target).parent().parent().find(&#39;textarea&#39;);
    if(textArea.css(&#39;display&#39;) == &quot;block&quot;){
        textArea.hide()
    }else{
        textArea.show()
    }
})

<!-- language: lang-css -->

.multipleNameReq{
  display:block
}

<!-- language: lang-html -->

&lt;div&gt;
  &lt;div class=&quot;input-group mb-3&quot;&gt;
      &lt;input type=&quot;text&quot; name=&quot;tname[]&quot; class=&quot;form-control&quot; required placeholder=&quot;req. for&quot;&gt;
      &lt;button class=&quot;btn btn-outline-secondary buttonMultipleNameReq&quot; type=&quot;button&quot; id=&quot;button-addon2&quot;&gt;hide/show&lt;/button&gt;
  &lt;/div&gt;
  &lt;textarea class=&quot;form-control multipleNameReq&quot;&gt;&lt;/textarea&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div class=&quot;input-group mb-3&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;tname[]&quot; class=&quot;form-control&quot; required placeholder=&quot;req. for&quot;&gt;
    &lt;button class=&quot;btn btn-outline-secondary buttonMultipleNameReq&quot; type=&quot;button&quot; id=&quot;button-addon2&quot;&gt;hide/show&lt;/button&gt;
&lt;/div&gt;
&lt;textarea class=&quot;form-control multipleNameReq&quot;&gt;&lt;/textarea&gt;

&lt;/div&gt;

&lt;div&gt;
&lt;div class=&quot;input-group mb-3&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;tname[]&quot; class=&quot;form-control&quot; required placeholder=&quot;req. for&quot;&gt;
    &lt;button class=&quot;btn btn-outline-secondary buttonMultipleNameReq&quot; type=&quot;button&quot; id=&quot;button-addon2&quot;&gt;hide/show&lt;/button&gt;
&lt;/div&gt;
&lt;textarea class=&quot;form-control multipleNameReq&quot;&gt;&lt;/textarea&gt;

&lt;/div&gt;
                
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

Using toggle function

here below is simpler code using jquery toggle, here you can read about toggle

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;body&#39;).on(&#39;click&#39;, &#39;.buttonMultipleNameReq&#39;,function(e){
	$(e.target).parent().parent().find(&#39;textarea&#39;).toggle();
})

<!-- language: lang-html -->

&lt;div&gt;
  &lt;div class=&quot;input-group mb-3&quot;&gt;
      &lt;input type=&quot;text&quot; name=&quot;tname[]&quot; class=&quot;form-control&quot; required placeholder=&quot;req. for&quot;&gt;
      &lt;button class=&quot;btn btn-outline-secondary buttonMultipleNameReq&quot; type=&quot;button&quot; id=&quot;button-addon2&quot;&gt;hide/show&lt;/button&gt;
  &lt;/div&gt;
  &lt;textarea class=&quot;form-control multipleNameReq&quot;&gt;&lt;/textarea&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div class=&quot;input-group mb-3&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;tname[]&quot; class=&quot;form-control&quot; required placeholder=&quot;req. for&quot;&gt;
    &lt;button class=&quot;btn btn-outline-secondary buttonMultipleNameReq&quot; type=&quot;button&quot; id=&quot;button-addon2&quot;&gt;hide/show&lt;/button&gt;
&lt;/div&gt;
&lt;textarea class=&quot;form-control multipleNameReq&quot;&gt;&lt;/textarea&gt;

&lt;/div&gt;

&lt;div&gt;
&lt;div class=&quot;input-group mb-3&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;tname[]&quot; class=&quot;form-control&quot; required placeholder=&quot;req. for&quot;&gt;
    &lt;button class=&quot;btn btn-outline-secondary buttonMultipleNameReq&quot; type=&quot;button&quot; id=&quot;button-addon2&quot;&gt;hide/show&lt;/button&gt;
&lt;/div&gt;
&lt;textarea class=&quot;form-control multipleNameReq&quot;&gt;&lt;/textarea&gt;

&lt;/div&gt;
                
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案2

得分: 0

以下是翻译好的部分:

To get element:
$('#multipleNameReq');

To Check if element is visible and not hidden:
$('#multipleNameReq').is(":visible");

To Show:
$('#multipleNameReq').show();

To Hide:
$('#multipleNameReq').hide();

A solution to add into your click function would look like:

if ($('#multipleNameReq').is(":visible")) {
$('#multipleNameReq').hide();
} else {
$('#multipleNameReq').show();
}

英文:

Since you're using jQuery you could use it's syntax:

To get element:
$(&#39;#multipleNameReq&#39;);

To Check if element is visible and not hidden:
$(&#39;#multipleNameReq&#39;).is(&quot;:visible&quot;);

To Show:
$(&#39;#multipleNameReq&#39;).show();

To Hide:
$(&#39;#multipleNameReq&#39;).hide();

A solution to add into your click function would look like:

 if ($(&#39;#multipleNameReq&#39;).is(&quot;:visible&quot;)) {
    $(&#39;#multipleNameReq&#39;).hide();
 } else {
    $(&#39;#multipleNameReq&#39;).show();
 }

答案3

得分: 0

要访问和更改元素的样式,您需要使用 css 函数。

$('body').on('click', '.buttonMultipleNameReq', function() {
  const element1 = $(this);
  const target1 = element1.parent().parent().find("#multipleNameReq");
  if (target1.css('display') === "none") {
    target1.css('display', 'block');
  } else {
    target1.css('display', 'none');
  }
})
<div>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <div class="input-group mb-3">
    <input type="text" name="tname[]" class="form-control" required placeholder="req. for">
    <button class="btn btn-outline-secondary buttonMultipleNameReq" type="button" id="button-addon2">Click<i class="bi bi-plus-circle"></i></button>
  </div>
  <textarea class="form-control" id="multipleNameReq" style="display: block;"></textarea>
</div>

这是代码部分的中文翻译。

英文:

To access and change the style of an element you need to use the css function.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;body&#39;).on(&#39;click&#39;, &#39;.buttonMultipleNameReq&#39;, function() {
  const element1 = $(this);
  const target1 = element1.parent().parent().find(&quot;#multipleNameReq&quot;);
  if (target1.css(&#39;display&#39;) === &quot;none&quot;) {
    target1.css(&#39;display&#39;, &quot;block&quot;);
  } else {
    target1.css(&#39;display&#39;, &quot;none&quot;);
  }
})

<!-- language: lang-html -->

&lt;div&gt;
  &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
  &lt;div class=&quot;input-group mb-3&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;tname[]&quot; class=&quot;form-control&quot; required placeholder=&quot;req. for&quot;&gt;
    &lt;button class=&quot;btn btn-outline-secondary buttonMultipleNameReq&quot; type=&quot;button&quot; id=&quot;button-addon2&quot;&gt;Click&lt;i class=&quot;bi bi-plus-circle&quot;&gt;&lt;/i&gt;&lt;/button&gt;
  &lt;/div&gt;
  &lt;textarea class=&quot;form-control&quot; id=&quot;multipleNameReq&quot; style=&quot;display: block;&quot;&gt;&lt;/textarea&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月10日 12:24:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定