我不能在修改文本后设置文本。

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

I can't set text after modifying the text

问题

在以下代码中,如果您单击任何表格行,信息将被写入文本区域,如果您单击temizleEn:"clean")按钮,则所有文本区域都将被清除。但是,如果您在文本中更改了内容后,不能设置或清除文本区域。

我尚未修复此错误。我正在使用jQuery进行选择,我认为这可能是问题的原因?

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

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

$("#hastaliklar tr").click(function() {
  $(this).addClass('selected').siblings().removeClass('selected');
  var ad = $(this).find('td:eq(1)').html();
  var tanim = $(this).find('td:eq(2)').html();
  var belirti = $(this).find('td:eq(3)').html();
  var yap = $(this).find('td:eq(4)').html();
  var yapma = $(this).find('td:eq(5)').html();
  $("#adtxt").text(ad);
  $("#tanimtxt").text(tanim);
  $("#belirtitxt").text(belirti);
  $("#yaptxt").text(yap);
  $("#yapmatxt").text(yapma);
  $("#kaydetbtn").addClass("unvisible");
  $("#kaydetbtn").prop('readonly', true);
  $("#guncellebtn").removeClass("unvisible");
});

$("#temizle").click(function() {
  $("#adtxt").text("");
  $("#tanimtxt").text("");
  $("#belirtitxt").text("");
  $("#yaptxt").text("");
  $("#yapmatxt").text("");
  $("#guncellebtn").addClass("unvisible");
  $("#guncellebtn").prop('readonly', true);
  $("#kaydetbtn").removeClass("unvisible");
});

$(document).ready(function() {
  $('#hastaliklar').DataTable();
});

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

.table-container {
  overflow: hidden;
}

.table-container table {
  table-layout: fixed;
  width: 100%;
  border-collapse: collapse;
}

.table-container td {
  max-width: 300px;
  min-height: 40px;
  max-height: 80px;
  white-space: normal;
  text-overflow: ellipsis;
}

.unvisible {
  background: transparent;
  border: none !important;
  font-size: 0;
}

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

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.2.0/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.4/css/dataTables.bootstrap5.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.js" type="text/javascript"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.5.0/font/bootstrap-icons.css">
<script type="text/javascript" src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.13.4/js/dataTables.bootstrap5.min.js"></script>
<form>
  <div class="input-group mb-3">
    <span class="input-group-text" id="inputGroup-sizing-default">Adı</span>
    <textarea class="form-control" id="adtxt" aria-label="With textarea"></textarea>
  </div>
  <div class="input-group mb-3">
    <span class="input-group-text" id="inputGroup-sizing-default">Tanımı</span>
    <textarea class="form-control" id="tanimtxt" aria-label="With textarea"></textarea>
  </div>
  <div class="input-group mb-3">
    <span class="input-group-text" id="inputGroup-sizing-default">Belirtileri</span>
    <textarea class="form-control" id="belirtitxt" aria-label="With textarea"></textarea>
  </div>
  <div class="input-group mb-3">
    <span class="input-group-text" id="inputGroup-sizing-default">Yapılması Gerekenler</span>
    <textarea class="form-control" id="yaptxt" aria-label="With textarea"></textarea>
  </div>
  <div class="input-group mb-3"&gt

<details>
<summary>英文:</summary>

In the below code, if you click any table row the information will be written to textareas, and if you click on the *temizle* (*En*: &quot;clean&quot;) button all textareas are going to be cleared. But, you can&#39;t set or clear the textarea after you change something in the text.

I didn&#39;t fix this bug yet. I&#39;m using jQuery for selecting, and I think maybe this could be the problem? 

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    $(&quot;#hastaliklar tr&quot;).click(function() {
      $(this).addClass(&#39;selected&#39;).siblings().removeClass(&#39;selected&#39;);
      var ad = $(this).find(&#39;td:eq(1)&#39;).html();
      var tanim = $(this).find(&#39;td:eq(2)&#39;).html();
      var belirti = $(this).find(&#39;td:eq(3)&#39;).html();
      var yap = $(this).find(&#39;td:eq(4)&#39;).html();
      var yapma = $(this).find(&#39;td:eq(5)&#39;).html();
      $(&quot;#adtxt&quot;).text(ad);
      $(&quot;#tanimtxt&quot;).text(tanim);
      $(&quot;#belirtitxt&quot;).text(belirti);
      $(&quot;#yaptxt&quot;).text(yap);
      $(&quot;#yapmatxt&quot;).text(yapma);
      $(&quot;#kaydetbtn&quot;).addClass(&quot;unvisible&quot;);
      $(&quot;#kaydetbtn&quot;).prop(&#39;readonly&#39;, true);
      $(&quot;#guncellebtn&quot;).removeClass(&quot;unvisible&quot;);
    });

    $(&quot;#temizle&quot;).click(function() {
      $(&quot;#adtxt&quot;).text(&quot;&quot;);
      $(&quot;#tanimtxt&quot;).text(&quot;&quot;);
      $(&quot;#belirtitxt&quot;).text(&quot;&quot;);
      $(&quot;#yaptxt&quot;).text(&quot;&quot;);
      $(&quot;#yapmatxt&quot;).text(&quot;&quot;);
      $(&quot;#guncellebtn&quot;).addClass(&quot;unvisible&quot;);
      $(&quot;#guncellebtn&quot;).prop(&#39;readonly&#39;, true);
      $(&quot;#kaydetbtn&quot;).removeClass(&quot;unvisible&quot;);
    });

    $(document).ready(function() {
      $(&#39;#hastaliklar&#39;).DataTable();
    });

&lt;!-- language: lang-css --&gt;

    .table-container {
      overflow: hidden;
    }

    .table-container table {
      table-layout: fixed;
      width: 100%;
      border-collapse: collapse;
    }

    .table-container td {
      max-width: 300px;
      min-height: 40px;
      max-height: 80px;
      white-space: normal;
      text-overflow: ellipsis;
    }

    .unvisible {
      background: transparent;
      border: none !important;
      font-size: 0;
    }

&lt;!-- language: lang-html --&gt;

    &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM&quot; crossorigin=&quot;anonymous&quot;&gt;
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.2.0/css/bootstrap.min.css&quot;&gt;
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://cdn.datatables.net/1.13.4/css/dataTables.bootstrap5.min.css&quot;&gt;
    &lt;script src=&quot;https://code.jquery.com/jquery-3.5.1.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap-icons@1.5.0/font/bootstrap-icons.css&quot;&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;https://cdn.datatables.net/1.13.4/js/dataTables.bootstrap5.min.js&quot;&gt;&lt;/script&gt;
    &lt;form&gt;
      &lt;div class=&quot;input-group mb-3&quot;&gt;
        &lt;span class=&quot;input-group-text&quot; id=&quot;inputGroup-sizing-default&quot;&gt;Adı&lt;/span&gt;
        &lt;textarea class=&quot;form-control&quot; id=&quot;adtxt&quot; aria-label=&quot;With textarea&quot;&gt;&lt;/textarea&gt;
      &lt;/div&gt;
      &lt;div class=&quot;input-group mb-3&quot;&gt;
        &lt;span class=&quot;input-group-text&quot; id=&quot;inputGroup-sizing-default&quot;&gt;Tanımı&lt;/span&gt;
        &lt;textarea class=&quot;form-control&quot; id=&quot;tanimtxt&quot; aria-label=&quot;With textarea&quot;&gt;&lt;/textarea&gt;
      &lt;/div&gt;
      &lt;div class=&quot;input-group mb-3&quot;&gt;
        &lt;span class=&quot;input-group-text&quot; id=&quot;inputGroup-sizing-default&quot;&gt;Belirtileri&lt;/span&gt;
        &lt;textarea class=&quot;form-control&quot; id=&quot;belirtitxt&quot; aria-label=&quot;With textarea&quot;&gt;&lt;/textarea&gt;
      &lt;/div&gt;
      &lt;div class=&quot;input-group mb-3&quot;&gt;
        &lt;span class=&quot;input-group-text&quot; id=&quot;inputGroup-sizing-default&quot;&gt;Yapılması Gerekenler&lt;/span&gt;
        &lt;textarea class=&quot;form-control&quot; id=&quot;yaptxt&quot; aria-label=&quot;With textarea&quot;&gt;&lt;/textarea&gt;
      &lt;/div&gt;
      &lt;div class=&quot;input-group mb-3&quot;&gt;
        &lt;span class=&quot;input-group-text&quot; id=&quot;inputGroup-sizing-default&quot;&gt;Yapılmaması Gerekenler&lt;/span&gt;
        &lt;textarea class=&quot;form-control&quot; id=&quot;yapmatxt&quot; aria-label=&quot;With textarea&quot;&gt;&lt;/textarea&gt;
      &lt;/div&gt;
      &lt;div class=&quot;input-group mb-3&quot;&gt;
        &lt;button id=&quot;kaydetbtn&quot; class=&quot;btn btn-success me-3 &quot; type=&quot;button&quot;&gt;Kaydet&lt;/button&gt;
        &lt;button id=&quot;guncellebtn&quot; class=&quot;btn btn-warning me-3&quot; type=&quot;button&quot;&gt;G&#252;ncelle&lt;/button&gt;
        &lt;button class=&quot;btn btn-danger&quot; id=&quot;temizle&quot; type=&quot;button&quot;&gt;Temizle&lt;/button&gt;
      &lt;/div&gt;
    &lt;/form&gt;
    &lt;table id=&quot;hastaliklar&quot; class=&quot; table table-bordered  table-primary&quot; style=&quot;overflow: auto; height: 400px; display: block; &quot;&gt;
      &lt;thead&gt;
        &lt;tr&gt;
          &lt;th&gt;G&#252;ncelle&lt;/th&gt;
          &lt;th&gt;Hastalık Adı&lt;/th&gt;
          &lt;th&gt;Tanımı&lt;/th&gt;
          &lt;th&gt;Belirtileri&lt;/th&gt;
          &lt;th&gt;Yapılması Gerekenler&lt;/th&gt;
          &lt;th&gt;Yapılmaması Gerekenler&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;button type=&quot;button&quot; onclick=&quot;veri_cek(1)&quot; class=&quot;btn btn-primary&quot;&gt;&lt;i class=&quot;bi bi-x-circle-fill&quot;&gt;&lt;/i&gt;&lt;/button&gt;&lt;/td&gt;
          &lt;td&gt;Demans&lt;/td&gt;
          &lt;td&gt;Yaşlılık&lt;/td&gt;
          &lt;td&gt;BelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileri&lt;/td&gt;
          &lt;td&gt;Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler
            Yapılması gerekenler Yapılması gerekenler &lt;/td&gt;
          &lt;td&gt;Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler &lt;/td&gt;

        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;button type=&quot;button&quot; onclick=&quot;veri_cek(1)&quot; class=&quot;btn btn-primary&quot;&gt;&lt;i class=&quot;bi bi-x-circle-fill&quot;&gt;&lt;/i&gt;&lt;/button&gt;&lt;/td&gt;
          &lt;td&gt;Demans&lt;/td&gt;
          &lt;td&gt;Yaşlılık&lt;/td&gt;
          &lt;td&gt;BelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileri&lt;/td&gt;
          &lt;td&gt;Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler
            Yapılması gerekenler Yapılması gerekenler &lt;/td&gt;
          &lt;td&gt;Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler &lt;/td&gt;

        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;button type=&quot;button&quot; onclick=&quot;veri_cek(1)&quot; class=&quot;btn btn-primary&quot;&gt;&lt;i class=&quot;bi bi-x-circle-fill&quot;&gt;&lt;/i&gt;&lt;/button&gt;&lt;/td&gt;
          &lt;td&gt;Demans&lt;/td&gt;
          &lt;td&gt;Yaşlılık&lt;/td&gt;
          &lt;td&gt;BelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileriBelirtileri&lt;/td&gt;
          &lt;td&gt;Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler Yapılması gerekenler
            Yapılması gerekenler Yapılması gerekenler &lt;/td&gt;
          &lt;td&gt;Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler Yapılmaması Gerekenler &lt;/td&gt;

        &lt;/tr&gt;

      &lt;/tbody&gt;
    &lt;/table&gt;
    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js&quot; integrity=&quot;sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.min.js&quot; integrity=&quot;sha384-fbbOQedDUMZZ5KreZpsbe1LCZPVmfTnH7ois6mU1QK+m14rQ1l2bGBq41eYeM/fS&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

&lt;!-- end snippet --&gt;



</details>


# 答案1
**得分**: 1

我认为代码的问题在于你尝试使用.text()方法来更改文本区域的内容,而正确的方法是.val()。.text()方法用于设置或返回所选元素的文本内容,而.val()用于获取匹配元素集合中第一个元素的当前值,或设置每个匹配元素的值。

请尝试这样做,然后告诉我们是否能够解决这个问题:

```javascript
$("#hastaliklar tr").click(function() {
  $(this).addClass('selected').siblings().removeClass('selected');
  var ad = $(this).find('td:eq(1)').html();
  var tanim = $(this).find('td:eq(2)').html();
  var belirti = $(this).find('td:eq(3)').html();
  var yap = $(this).find('td:eq(4)').html();
  var yapma = $(this).find('td:eq(5)').html();
  $("#adtxt").val(ad);
  $("#tanimtxt").val(tanim);
  $("#belirtitxt").val(belirti);
  $("#yaptxt").val(yap);
  $("#yapmatxt").val(yapma);
  $("#kaydetbtn").addClass("unvisible");
  $("#kaydetbtn").prop('readonly', true);
  $("#guncellebtn").removeClass("unvisible");
});

$("#temizle").click(function() {
  $("#adtxt").val("");
  $("#tanimtxt").val("");
  $("#belirtitxt").val("");
  $("#yaptxt").val("");
  $("#yapmatxt").val("");
  $("#guncellebtn").addClass("unvisible");
  $("#guncellebtn").prop('readonly', true);
  $("#kaydetbtn").removeClass("unvisible");
});

$(document).ready(function() {
  $('#hastaliklar').DataTable();
});

希望这对你有帮助。

英文:

I think the issue with the code is that you are trying to change the content of a textarea with the .text() method, while the correct method is .val(). The .text() method is used to set or return the text content of selected elements, while .val() is used to get the current value of the first element in the set of matched elements or set the value of every matched element.

Please try this and let us know if you are able to solve this issue:

    $(&quot;#hastaliklar tr&quot;).click(function() {
$(this).addClass(&#39;selected&#39;).siblings().removeClass(&#39;selected&#39;);
var ad = $(this).find(&#39;td:eq(1)&#39;).html();
var tanim = $(this).find(&#39;td:eq(2)&#39;).html();
var belirti = $(this).find(&#39;td:eq(3)&#39;).html();
var yap = $(this).find(&#39;td:eq(4)&#39;).html();
var yapma = $(this).find(&#39;td:eq(5)&#39;).html();
$(&quot;#adtxt&quot;).val(ad);
$(&quot;#tanimtxt&quot;).val(tanim);
$(&quot;#belirtitxt&quot;).val(belirti);
$(&quot;#yaptxt&quot;).val(yap);
$(&quot;#yapmatxt&quot;).val(yapma);
$(&quot;#kaydetbtn&quot;).addClass(&quot;unvisible&quot;);
$(&quot;#kaydetbtn&quot;).prop(&#39;readonly&#39;, true);
$(&quot;#guncellebtn&quot;).removeClass(&quot;unvisible&quot;);
});
$(&quot;#temizle&quot;).click(function() {
$(&quot;#adtxt&quot;).val(&quot;&quot;);
$(&quot;#tanimtxt&quot;).val(&quot;&quot;);
$(&quot;#belirtitxt&quot;).val(&quot;&quot;);
$(&quot;#yaptxt&quot;).val(&quot;&quot;);
$(&quot;#yapmatxt&quot;).val(&quot;&quot;);
$(&quot;#guncellebtn&quot;).addClass(&quot;unvisible&quot;);
$(&quot;#guncellebtn&quot;).prop(&#39;readonly&#39;, true);
$(&quot;#kaydetbtn&quot;).removeClass(&quot;unvisible&quot;);
});
$(document).ready(function() {
$(&#39;#hastaliklar&#39;).DataTable();
});

huangapple
  • 本文由 发表于 2023年6月13日 05:35:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460467.html
匿名

发表评论

匿名网友

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

确定