简体   繁体   English

使用 JavaScript 获取带有会话用户输入值的 HTML 字符串

[英]Use JavaScript to get HTML string with session user input values

I am currently working on a project where I need to be able to handle storing and viewing HTML strings (from a database, but from the page itself for testing) which have input elements in them.我目前正在开发一个项目,在该项目中我需要能够处理存储和查看 HTML 字符串(来自数据库,但来自用于测试的页面本身),其中包含输入元素。 I also need to store the current input values (what the user has placed in them while the user is viewing the page) of the elements, preferably within the HTML string.我还需要存储元素的当前输入值(用户在查看页面时放入的内容),最好是在 HTML 字符串中。

Here is an example of what I looking for help with:以下是我寻求帮助的示例:

I have the following HTML displaying:我有以下 HTML 显示:

<p id="pullfrom">
Character Name: <input><br>
Character Level: <input type="number">
</p>
<button onclick="algorithm('pullfrom')">Save</button>

The user then enters a character name of "Garrus Vakarian" into the text box and "5" into the number box.用户然后在文本框中输入“Garrus Vakarian”的字符名称,在数字框中输入“5”。 Then the user presses the save button which calls the algorithm.然后用户按下调用算法的保存按钮。

The algorithm then returns:然后算法返回:

<p id="pullfrom">
Character Name: <input value="Garrus Vakarian"><br>
Character Level: <input type="number" value="5">
</p>

Using setAttribute( "value", npt.value )使用setAttribute( "value", npt.value )

Sets the value of an attribute on the specified element.设置指定元素的属性值。 If the attribute already exists, the value is updated;如果属性已经存在,则更新值; otherwise a new attribute is added with the specified name and value.否则,将添加具有指定名称和值的新属性。

forEach <input> in the element being parsed, we can set the HTML value attribute to the value supplied, then get the outerHTML toString() . forEach <input>中的元素被解析,我们可以设置HTML value属性的value提供,则得到outerHTML toString()

 function algorithm( id ) { const e = document.getElementById( id ), npts = e.querySelectorAll( "input" ); npts?.forEach( npt => npt.setAttribute( "value", npt.value ) ); console.log( e.outerHTML.toString() ); }
 <p id="pullfrom"> Character Name: <input><br> Character Level: <input type="number"> </p> <button onclick="algorithm('pullfrom')">Save</button>

Note: this is a terrible way of gathering form data and torturing the DOM like this should probably be a crime.注意:这是一种收集表单数据并像这样折磨 DOM 的可怕方式,应该是一种犯罪。 However...然而...

For more complex and standard <form> s对于更复杂和标准的<form> s

Although the following example isn't a very complex <form> , it should demonstrate how this method might be expanded to handle different types of form data.尽管下面的示例不是一个非常复杂的<form> ,但它应该演示如何扩展此方法以处理不同类型的表单数据。

 const frm = document.getElementById( "demo" ); frm.submit.addEventListener( "click", () => { // grab all desired inputs by tag const npts = frm.querySelectorAll( 'input:not([type="button"]), select, textarea' ); npts?.forEach( npt => { switch ( npt.tagName.toLowerCase() ) { case "input": { switch ( npt.type ) { case "radio": case "checkbox": { npt.removeAttribute( "checked" ); // remove prior selections npt.setAttribute( "checked", npt.checked ); break; } default: npt.setAttribute( "value", npt.value ); } break; } case "textarea": npt.textContent = npt.value; break; case "select": const optns = npt.querySelectorAll( "option" ), pre_slctd = npt.querySelector( "[selected]" ); if ( pre_slctd ) { pre_slctd.removeAttribute( "selected" ); // remove prior selections } optns[ npt.selectedIndex ].setAttribute( "selected", "selected" ); break; } } ); console.log( frm.outerHTML.toString() ); }, { passive: true } );
 label { display: block; margin-bottom: .3em; }
 <form id="demo"> <label>Character name: <input name="char_name" type="text"></label> <label>Character level: <input name="char_level" type="number"></label> <label>Character species: <select name="char_species"> <option value="imagination">Human</option> <option value="cuddly" selected>Anthro</option> <option value="target_practice">Undead</option> <option value="caffeine_sponge">Developer</option> </select></label> <fieldset> <legend>Character's favourite radio station</legend> <label>Electro: <input type="radio" name="char_radio" value="electro" checked></label> <label>Black metal: <input type="radio" name="char_radio" value="black_metal"></label> <label>Opera: <input type="radio" name="char_radio" value="opera"></label> </fieldset> <label>Character has character: <input name="char_char" type="checkbox"></label> <label for="bio">Bio:</label> <textarea name="char_bio" id="bio"></textarea> <label>I do not understand the Terms of Service because I have not read them: <input name="tos" type="checkbox" required></label> <input name="submit" type="button" value="Submit"> </form>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM