简体   繁体   English

如何在按钮单击上添加文本框?

[英]How to add a text box on a button click?

I am writing a JSP page where an admin can prepare the question format for the users. 我正在编写一个JSP页面,管理员可以在其中为用户准备问题格式。 All the questions have multiple choices ranging from 2 (for yes or no) to 5. I want to provide a button for the admin which on click has to generate a text box corresponding to 1 answer. 所有问题都有从2(是或否)到5的多个选择。我想为管理员提供一个按钮,单击该按钮时必须生成一个文本框,对应于1个答案。 This button should also check that the answer boxes have not exceeded 5 as there can be no more than 5 multiple choices. 此按钮还应检查答案框是否超过5个,因为最多只能选择5个。

How can I go about this problem? 我该如何解决这个问题? How to stuff the JavaScript variable inside the innerHTML ? 如何将JavaScript变量填充到innerHTML I have done something like this: 我做了这样的事情:

function addAnswer(queryNum, ansCount){
    var d=document.getElementById("div");
    var i = queryNum;
    alert(i);   
    var j = ansCount;
    alert(j);   
    d.innerHTML+="<label class='optsurvey-answer' for='answer_'>Answer</label>"+"<input class='optsurvey-answerinput' type='text' name='<portlet:namespace />answer_'>";
}
</script>

Here queryNum and ansCount are params passed by JSP. 这里的queryNumansCount是JSP传递的参数。 I want to stuff them inside the d.innerHTML . 我想将它们d.innerHTML Something like this 像这样

d.innerHTML+="<label class='optsurvey-answer' for='answer_'>Answer<HERE I HAVE TO STUFF "ansCOUNT" ></label>"+"<input class='optsurvey-answerinput' type='text' name='<portlet:namespace />answer_'>";

You are going to need to use JavaScript. 您将需要使用JavaScript。 You might want to start by reading up on JavaScript and DHTML. 您可能想开始阅读JavaScript和DHTML。 This would be a good place to start: http://www.devx.com/codemag/Article/15585 这将是一个不错的起点: http : //www.devx.com/codemag/Article/15585

You need to get this working like below mentioned methods 1) Write a Javascript which displays the text box. 您需要像下面提到的方法那样进行此工作:1)编写一个显示文本框的Javascript。 When do we call the above javs script? 我们什么时候调用上面的javs脚本? ON click of the button call the JS and Show it to the user 2) on Action, send the values from the jsp to the servlets or Action class for processing. 单击按钮后,调用JS并将其显示给用户2)在Action上,将jsp中的值发送到servlet或Action类进行处理。

I hope this is clear, else dont hesitate to put ur comments, will reply you 我希望这很清楚,否则请不要犹豫,发表评论,将回复您

Like in Java you can concatenate strings in JavaScript using the + operator. 像Java中一样,您可以使用+运算符在JavaScript中连接字符串。

var foo = 'some JS string';
div.innerHTML += '<someElement>' + foo + '</someElement>';

To learn more about JavaScript, I recommend to go through this w3schools quick tutorial/reference . 要了解有关JavaScript的更多信息,建议您阅读本w3schools快速教程/参考 If you want to go a bit further, go through this advanced presentation/exercise of John Resig . 如果您想进一步讲解 ,请阅读John Resig的高级演示/练习 If you want a genious and easy to-use JavaScript framework, have a look at jQuery . 如果您想要一个简洁且易于使用的JavaScript框架,请查看jQuery If you want to learn more about communication between Java/JSP and JavaScript, check this article . 如果您想了解有关Java / JSP与JavaScript之间的通信的更多信息, 请查看本文

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

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