简体   繁体   English

您如何在jQuery中编辑HTML表单元素?

[英]How do do you edit HTML form elements’ in jQuery?

I'm trying to make a simple font generator for a challenge. 我正在尝试制作一个简单的字体生成器来应对挑战。 The form element I'm trying to edit is the tag. 我要编辑的表单元素是标签。

The way this webpage is supposed to work is a user types in the “input textbox”, presses one of the 3 buttons and the new text will appear in the “output textbox” in the font that the user chooses. 该网页的工作方式是用户在“输入文本框”中键入,按三个按钮之一,新文本将以用户选择的字体显示在“输出文本框”中。

This is what my HTML looks like: 这是我的HTML外观:

<div id=“inputContainer”>
    <p id=“inputText style=“text-decoration:underline;position:relative;”>Input</p>
    <textarea id=“input” rows=“10” columns=“10”></textarea>
</div>
<div id=“outputContainer”>
    <p id=“outputText” style=“text-decoration:underline;position:relative;”>Output</p>
    <textarea id=“output” rows=“10” columns=10”></textarea>
</div>

And here is my jQuery/JS: 这是我的jQuery / JS:

var inputText = $("#input").html();
    $("#monospaceButton").click(function() {
    $("#output").html(inputText);
});

The function only works if I manually add text between the tags (innerHTML). 仅当我在标签(innerHTML)之间手动添加文本时,该功能才有效。 When the user types in the textarea in the code's output (The website.), there will be no output in the “output” textarea. 当用户在代码输出(网站)中输入文本区域时,“输出”文本区域中将没有输出。

I appreciate any help. 感谢您的帮助。 Please let me know if I need to explain anything more clearly. 如果需要进一步说明,请告诉我。 Thanks! 谢谢!

 $("#button1").click(function() { var value = $("#input").val(); $("#output").css('font-family', 'serif'); $("#output").val(value); }); $("#button2").click(function() { var value = $("#input").val(); $("#output").css('font-family', 'monospace'); $("#output").val(value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <p id="inputText" style="text-decoration:underline;position:relative;">Input</p> <textarea id="input" rows="3" columns="10"></textarea> </div> <button id="button1">Style 1</button> <button id="button2">Style 2</button> <div> <p id="outputText" style="text-decoration:underline;position:relative;">Output</p> <textarea id="output" rows="3" columns="10"></textarea> </div> 

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

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