简体   繁体   English

如何添加一旦输入值将更新的输入字段?

[英]How to add input fields that will update once values are entered?

I am creating a javascript for creating SVG graphs based on user-input parameters, and I would like to add some input fields that can accept numeric input and string input. 我正在创建一个JavaScript,用于根据用户输入的参数创建SVG图,并且我想添加一些可以接受数字输入和字符串输入的输入字段。 Imagine something like: 想象一下:

Graph height: [600]
Background Color: [#dfe0ef]
...

^ Imagine that instead of brackets those are text fields on the page. ^ 想象一下,不是括号,而是页面上的文本字段。

I was wondering if I should use a form for this. 我想知道是否应该为此使用表格。 But I don't need to send any data to a server, so I would like to stick with only js if possible to keep it simple. 但是我不需要将任何数据发送到服务器,因此我想尽可能地坚持使用js。 I do need the SVG graph to update in the browser every time a new value is entered. 每当输入新值时,我确实需要在浏览器中更新SVG图。

All of the examples I've found so far are more complex than what I need, so it has been difficult to use them as references. 到目前为止,我发现的所有示例都比我需要的复杂,因此很难将它们用作参考。 So hopefully somebody here can explain: How to add input fields that will update the script (and therefor the SVG) once entered? 因此希望这里的人可以解释一下:如何添加输入字段,一旦输入将更新脚本(以及SVG)?

Thanks 谢谢

Update: I've got a proof-of-concept working with a DIV tag. 更新:我有一个使用DIV标签的概念验证。 So now all that's left is to replace the DIV with an SVG and populate it with more parameters. 所以现在剩下的就是用SVG替换DIV并用更多参数填充它。

 <!DOCTYPE html> <html> <body> <p>Please specify a width, a height, and a background color.</p> <p>Width:</p> <input id="i_width" type="number" min="1" max="1024" value="200" oninput="render()" > <br /> <p>Height:</p> <input id="i_height" type="number" min="1" max="1024" value="100" oninput="render()" > <br /> <p>Background Color:</p> <input id="i_bg_color" type="text" value="#bada55" oninput="render()" > <p id="text_output"></p> <br /> <div id="container_output"></div> <script> //Gets all variable values from input fields. //Input IDs are "i_" + "var_name" function render() { var width = document.getElementById("i_width").value; var height = document.getElementById("i_height").value; var bg_color = document.getElementById("i_bg_color").value; //text_output document.getElementById("text_output").innerHTML = "The width is " + width + ", and the height is " + height + ". The background color is " + bg_color + "."; //container_output document.getElementById("container_output").innerHTML = ` <div style=" width:${width}px; height:${height}px; background-color:${bg_color}; "> </div> ` } </script> </body> </html> 

You can attach listeners for the oninput event in JavaScript like so: 您可以像这样在JavaScript中为oninput事件附加侦听器:

text_field_element.addEventListener('oninput', () => {
    //redraw 
}

where text_field_element is some <input> element. 其中text_field_element是一些<input>元素。

Without seeing more details about your implementation, I'm not sure what to say about redrawing the actual graphs, though attaching that listener should get your foot in the door. 在没有看到有关实现的更多详细信息的情况下,我不确定要重画实际的图形该说些什么,尽管附加该侦听器应该会使您步入正轨。

This is how I would do it: 这就是我要做的:

 theHeight.addEventListener("input", () => { theRect.setAttributeNS(null, "height", theHeight.value); }); 
 svg{border:1px solid; width:90vh} 
 <svg viewBox="0 0 100 50"> <rect id="theRect" x="20" y="10" width="60" height="30" fill="skyBlue" /> </svg> <input id="theHeight" type="number" min="1" max="40" value="30" /> 

暂无
暂无

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

相关问题 显示动态生成的输入字段的输入值 - Displaying the entered values of dynamically generated input fields 如何根据在不同输入字段中输入的值添加输入字段的数量 - How to add number of input fields based on value entered on a different input field 您如何添加输入字段中的值并使用jQuery中的结果更新另一个字段? - How do you add the values from input fields and update another field with the result in jQuery? jQuery仅在输入字段中输入内容时才添加新字段 - jQuery to only add new fields when something is entered in the input fields Pug JS:如何在输入字段中保留输入的值而不是在验证后清除表单 - Pug JS: How to keep the entered values in input fields instead of clearing the form after validation 为行中的任何字段输入信息后,如何添加必填字段,该行中的所有字段也应为必填项 - How to add required filed once information has been entered for any field in the row, all fields in that row should be required too 根据 yii2 中输入的值更新金额和总计字段 - Update amount and total fields based on values entered in yii2 如何使用javascript更新隐藏输入字段的值 - How do I use javascript to update the values of hidden input fields 在输入字段中输入值后如何显示跨度? 角JS - How to display the span once the value is entered in input field? Angular JS 输入后如何提交用户输入并删除问题? - How to submit user input and remove question once entered?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM