【问题标题】:How to change the textarea's value with this scripted function?如何使用此脚本函数更改 textarea 的值?
【发布时间】:2018-07-19 08:48:25
【问题描述】:

我想通过点击网页按钮来改变 textarea 的值。

我在 textarea 中写了一些东西,然后单击按钮,这样功能就可以改变 textarea 中的文本。

但是,当我尝试此代码时,没有任何反应。

function Josa(txt, josa) {
  var txt = document.getElementById("QQQQQ");
  var code = txt.charCodeAt(txt.length - 1) - 44032;
  var cho = 19,
    jung = 21,
    jong = 28;
  var i1, i2, code1, code2;


  if (txt.length == 0) return '';


  if (code < 0 || code > 11171) return txt;

  if (code % 28 == 0) return txt + Josa.get(josa, false);
  else return txt + Josa.get(josa, true);
}
Josa.get = function(josa, jong) {


  if (josa == '을' || josa == '를') return (jong ? '을' : '를');
  if (josa == '이' || josa == '가') return (jong ? '이' : '가');
  if (josa == '은' || josa == '는') return (jong ? '은' : '는');
  if (josa == '와' || josa == '과') return (jong ? '와' : '과');


  return '**';
}
<textarea id="QQQQQ" rows="10" cols="100"></textarea><br/>
<button onclick="Josa()">convert!</button>

【问题讨论】:

  • 您是否尝试过像 function Josa() 这样从 Josa 函数中取出参数,因为您在任何时候都没有传递任何参数。
  • 获取文本,需要取值:txt = document.getElementById("QQQQQ").value;
  • var txt = document.getElementById("QQQQQ"); 将返回一个 HTML DOM 元素..您必须使用 txt.value 来获取输入,为什么不使用参数而不是 Josa()
  • 抱歉,但什么也没发生。 (尽管我遵循了你们所有人提到的一切)。我不知道?
  • 1.add -.value, 2.deleted-Josa(josa, txt)->Josa()。但一无所获。

标签: javascript textarea


【解决方案1】:
  1. txt = document.getElementById("QQQQQ").value; // 你需要 .value
  2. 您没有在任何地方使用返回值
    document.getElementById("QQQQQ").value=txt + Josa.get
  3. if (code % 28 == 0) return txt + Josa.get(josa, false); - josa 中没有任何内容

function Josa(txt, josa) {
  var txt = document.getElementById("QQQQQ").value;
  var code = txt.charCodeAt(txt.length - 1) - 44032;
  var cho = 19,
    jung = 21,
    jong = 28;
    
  var i1, i2, code1, code2;


  if (txt.length == 0) return '';
  console.log(code)

  if (code < 0 || code > 11171) return txt;

  document.getElementById("QQQQQ").value=txt + Josa.get(String.fromCharCode(code), code % 28 !== 0);
}
Josa.get = function(josa, jong) {
  if (josa == '을' || josa == '를') return (jong ? '을' : '를');
  if (josa == '이' || josa == '가') return (jong ? '이' : '가');
  if (josa == '은' || josa == '는') return (jong ? '은' : '는');
  if (josa == '와' || josa == '과') return (jong ? '와' : '과');
  return '**';
}
<textarea id="QQQQQ" rows="10" cols="100"></textarea><br/>
<button onclick="Josa()">convert!</button>

【讨论】:

  • 对不起,但是当我运行它时,却失败了。 '영등포은' ->.'영등포가' 应该是我所期望的结果。但是,当我运行这个时......'영등포은'->'영등포은**'。
  • 只添加了“**”字母,尽管这不应该发生。
  • 我不知道脚本应该产生什么。我无法阅读这些字符。至少我的脚本运行。现在逻辑需要修复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-05
  • 2020-06-30
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
  • 2019-10-17
相关资源
最近更新 更多