18910140161

JavaScript-如何在更改输入字段上更改图表JS标题文本?-堆栈溢出

顺晟科技

2022-10-19 12:44:16

152

我有一个图表JS,在它的顶部我有一个输入字段。当我想做的是让用户在输入字段上键入他想要的任何东西,当他在这个输入字段上键入完毕时。然后,他的图表JS标题文本将变为他在输入字段中所写的内容。所以在图片下面这个方框中包含“图表标题”是一个输入字段。我希望用户在那里键入,当它完成时,图表标题文本“这是一个图表”改变为输入字段中所写的内容。

HTML

<input  type="text" style="border:1px solid white;background-color: #E9E8E6; color: black;  text-align: center;" placeholder = "Chart Title">    <!--The input field that takes user input and when its done I want to change the chart JS title text which have the text "This is a chart" to be changed to the input field text -->
                                    <div class="card-body">
                                      <canvas id="barChart_3"></canvas>

JS

<input  type="text" style="border:1px solid white;background-color: #E9E8E6; color: black;  text-align: center;" placeholder = "Chart Title">    <!--The input field that takes user input and when its done I want to change the chart JS title text which have the text "This is a chart" to be changed to the input field text -->
                                    <div class="card-body">
                                      <canvas id="barChart_3"></canvas>


顺晟科技:

我不知道如何更改内置标题,但您可以隐藏或省略它,并使用常规标题元素,如果对您有效,那么这是一个有效的解决方案

一种方法是监听输入值的变化,然后在每次变化时抓取输入值并将其添加到标题中,如下所示:

我使用了事件,但如果您愿意,可以使用or事件。事件只会在输入值更改时和每次更改时触发。当用户按enter或离开输入字段时(输入字段失去焦点),只有当当前值与前一个值不相同时,用户才能看到更改,因为这样事件就不会触发,而事件会触发,例如。

您需要将您的变量放入在您的函数中可访问的变量中,该函数在用户完成键入后运行。

之后可以调用:

请参见fiddle链接,在此链接中,创建图表后,我将标题从FFFF更新为dddd:https://jsfiddle.net/leelenaleee/0vxCWQFT/4/

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航