When I click on the button will be equal to the amount of undefind while the amount is equal to 1
in jsp page :
<%
String err1 = (String) request.getAttribute("err2");
int code = 0;
if (err1 != null){
code = Integer.parseInt(err1);
System.out.println(" code " + code);
}
%>
<button type="button" id="btnok">ok</button>
<br />
<%if(code == 3){ %>
<input id="txtcode" type="hidden" value="1" />
<%}%>
<br />
in jquery code:
$(document).ready(function() {
$("#btnok").click(function(event) {
var xxx = $("#txtcode").val();
alert("xxx 1 test + " + xxx);
});
});
The most likely reason is that the condition:
<%if(code == 3){ %>
<input id="txtcode" type="hidden" value="1" />
<%}%>
is not being met and the input is not being inserted to the DOM. Reload the page and view the page source to see if the input is inserted.
Related
I want to update a particular column in my database with the word 'trash' but I need to first tick the values in check box. Then when u tick the checkbox, the system will only update the status field of the id's selected
public void updateNow(Messages message){
Query query = em.createNativeQuery("Update Messages set status = 'trash' WHERE id =:id");
query.setParameter("id", message.getId());
query.executeUpdate();
}
}
<%
//updating the filed
String[] arr = request.getParameterValues("content");
int sum=0;
if(request.getParameter("update")!=null){
try{
for (int i=0; i<arr.length; i++){
sl.updateNow(arr[i]);
sum++;
msg = "<script>alert('You have move message to trash')</script>";
}
}catch (Exception e){
msg="Please Do select an email before performing an action" + e.getMessage();
e.printStackTrace();
}
}
%>
<html>
<body>
<form action="" method="post">
<%
List inboxFetch = sl.allMessages(user, "inbox");
Iterator inboxIterate = inboxFetch.iterator();
while(inboxIterate.hasNext()){
Messages all = (Messages) inboxIterate.next();
%>
<input type="checkbox" value="<%=all.getId() %>" name="content">
<%
}
%>
<br />
<hr />
<button type="submit" name="update" class="btn btn-sm btn-danger">Update</button>
</form>
</body>
</html>
Please how do I achieve this? I have been on this code now for quite some hours seems I can't get through it
I am able to display a table when a user clicks a search button. But now I want to split the table into chunks of 20 rows where the user can click next or previous to go forward and backward through all of the data. I am not allowed to use JavaScript for this assignment. Only JSP, Java, HTML.
The two debugging out.print() calls are not showing up. A different page is being loaded after one of the buttons is clicked, but the two debugging out.print calls are not displaying any HTML. I have checked out How to know which button is clicked on jsp this post but had no luck.
<form method="GET">
<center>
<input type="submit" name="previous_table" value="Previous" />
<input type="submit" name="next_table" value="Next" />
</center>
</form>
</br>
<%
String val1 = request.getParameter("previous_table");
String val2 = request.getParameter("next_table");
try {
if ("Previous".equals(val1)) { // Get previous results
out.println("<h1>HELLO 1</h1>");
buildTable(rs, out, false);
}
else if ("Next".equals(val2)) { // Get next results
out.println("<h1>HELLO 2</h1>");
buildTable(rs, out, true);
}
} catch(Exception e) {
out.print("<center><h1>"+e.toString()+"</h1></center>");
}
%>
I also have a follow up question. If the user clicks next or previous button, will my current table on the page be overwritten by the new one? That is my intent but I don't know if it will work that way.
I WAS ABLE TO FIX IT BY DOING THIS:
<form method="POST">
<center>
<input type="submit" name="previous_table" value="Previous" />
<input type="submit" name="next_table" value="Next" />
</center>
</form>
</br>
<%
String val1 = request.getParameter("previous_table");
String val2 = request.getParameter("next_table");
you should add name with value for button after that you can get by parameter click value.
`<input type="hidden" name="myprevious" value="previous"/>
<input type="hidden" name="mynext" value="next" />
<%
String val1 = request.getParameter("myprevious");
String val2 = request.getParameter("mynext");
try {
if (val1 == "previous") { // Get previous results
out.println("<h1>HELLO 1</h1>");
buildTable(rs, out, false);
}
else if (val2 == "next") { // Go next results
out.println("<h1>HELLO 2</h1>");
buildTable(rs, out, true);
}
} catch(Exception e) {
out.print("<center><h1>"+e.toString()+"</h1></center>");
}
%>
`
I hope it will help you.
Thanks.
Try to use the subList() method of a List<>().
As explained here.
HOW TO IMPLEMENT IT ##
you can put an hidden input in your form to give you the last index for your list like :
<input type="hiden" value="${last_index_of_your_list + 1}" name="index">
Then in your servlet part you put like this :
int index = Interger.ParseInt(request.getParameter("index"));
if(index <= 0){
datalist = datalist(0, 19>datalist.size()? datalist.size() : 19);
}else{
if(clicked_on_next){
datalist = datalist(index, index+19>datalist.size()? datalist.size() : index+19 );
}else{
datalist = datalist(index - 40, index-20>datalist.size()? datalist.size() : index-20 );
}
}
You are using hidden fields but you need to use submit button for next and previous.
<input type="submit" name="myprevious" value="previous"/>
<input type="submit" name="mynext" value="next" />
Make sure both are define in form. when we submit form after that you will get button value in parameter.same my previous answer. because we can not get parameter value without submit form.
Thanks.
I have list checkboxes in jsp, now I just want to get value of some checkboxes have checked.
<%
for(DictItem dictItemDomain : dictItemDomains) {
%>
<aui:input
name="businessDomains"
id='<%= "businessDomain" + dictItemDomain.getDictItemId()%>'
value="<%=dictItemDomain.getItemCode() %>"
type="checkbox"
label="<%=dictItemDomain.getItemName(locale, true)%>"/>
<%
}
%>
controller :
String[] domains =
ParamUtil.getParameterValues(
actionRequest, "businessDomains");
but domains contain value of checked and unchecked together
<aui:input name="listBussinessDomains" type="hidden" value="" />
<%
for(DictItem dictItemDomain : dictItemDomains) {
%>
<aui:input
name="businessDomains"
id='<%= "businessDomain" + dictItemDomain.getDictItemId()%>'
value="<%=dictItemDomain.getItemCode() %>"
type="checkbox"
label="<%=dictItemDomain.getItemName(locale, true)%>"
cssClass="getval"
/>
<%
}
%>
I try to add hidden input and using jquery to make an Array value then I put it for value attr of hidden input, finally, retrieve in action controller.
<script>
AUI().ready(function(A) {
var businessTypeCbs = $(".getval");
var businessTypeCbsChecked = $(".getval:checked");
var checkedArr = [];
var listBussinessDomains = A.one("#<portlet:namespace />listBussinessDomains");
businessTypeCbsChecked.each(function() {
checkedArr.push($(this).attr("value"));
listBussinessDomains.val(checkedArr);
});
businessTypeCbs.click(function() {
if($(this).is(":checked")) {
//alert($(this).attr("value") + ' ' + $(this).attr("id"));
if($.inArray($(this).attr("value"), checkedArr) == -1) {
checkedArr.push($(this).attr("value"));
}
} else {
if($.inArray($(this).attr("value"), checkedArr) > -1) {
removeItem = $(this).attr("value");
checkedArr = $.grep(checkedArr, function(value) {
return value != removeItem;
});
}
}
listBussinessDomains.val(checkedArr);
});
});
in controller
String [] listBussinessDomains = ParamUtil
.getParameterValues(actionRequest, "listBussinessDomains");
WORKED!
In my jsp i have a variable
String str = "Get_Name";
What can i do to have my variable(str) directly accessed in the input type shown below? By this I want to see the form auto filled with the the variable I have instead of me filling it.
<input id="vendorName" name="vendorName" type="text" class="txtsmall2" />
ANSWER ALSO CONTAINS SOLUTION FOR PROBLEM DISCUSSED IN COMMENTS.
Do this.
<%
long val = 2; //can be anything.
if(val > 1) {
%>
<input id="vendorName" name="vendorName" type="text" class="txtsmall2" value="<%=str%>"/>
<%
else {
%>
<input id="vendorName" name="vendorName" type="text" class="txtsmall2" />
<%
}
%>
<%=str%> is called as expression tag.
I am developing an online test application and I am extracting all the questions from the
xml file to a jsp page...All the questions are displayed using a for loop in jsp...
Now i don`t want all the questions in a single page , rather i want them one by one to be displayed as the user clicks the "next" button . So here is the code what i am using and it is
displaying only the first and the 2nd question and from the 3rd question on ,, its fading in and again fading out ...without clicking the "next " button...need some suggestions here...thanks...
HERE IS THE CODE:
$(document).ready(function(){
var questions;
var counter;
var totalQuestions;
var currentQuestion=0;
var i=0;
totalQuestions=$("#javapaperlist").val();
questions=$(".questions");
questions.hide();
$(questions.get(currentQuestion)).fadeIn(1000);
$('#next').click(function(){
$(questions.get(currentQuestion)).fadeIn(1000);
$(questions.get(currentQuestion)).fadeOut(2000);
currentQuestion=currentQuestion+1;
$(questions.get(currentQuestion)).fadeIn(1000);
});
)};
METHOD="post">
<%
for(int i = 0; i < javapaperList.size(); i++){
JavaPaper paper = javapaperList.get(i);
String text = paper.getText();
StringTokenizer tokens = new StringTokenizer(text, "##");
int tokensint = tokens.countTokens();
%>
<div class="questions" id=<%out.println("qx" + paper.getId());%> style="display:none" >
<%
while(tokens.hasMoreTokens()){
%>
<label class="questiontext" ><%=tokens.nextToken()%></label><br>
<%
}
%>
<BR>
<BR>
<INPUT TYPE="radio" NAME=<%out.println("question" + paper.getId());%> VALUE="A" id=<% out.println("A" + paper.getId()); %> onClick="rboc(this)" class="option">
<% out.println(paper.getOptiona());%>
<BR>
<INPUT TYPE="radio" NAME=<%out.println("question" + paper.getId());%> VALUE="B" id=<% out.println("B" + paper.getId()); %> onClick="rboc(this)" class="option">
<% out.println(paper.getOptionb());%>
<BR>
<INPUT TYPE="radio" NAME=<%out.println("question" + paper.getId());%> VALUE="C" id=<% out.println("C" + paper.getId()); %> onClick="rboc(this)" class="option">
<% out.println(paper.getOptionc());%>
<BR>
<INPUT TYPE="radio" NAME=<%out.println("question" + paper.getId());%> VALUE="D" id=<% out.println("D" + paper.getId()); %> onClick="rboc(this)" class="option">
<% out.println(paper.getOptiond());%>
<BR>
<INPUT TYPE="radio" NAME=<%out.println("question" + paper.getId());%> VALUE="E" id=<% out.println("E" + paper.getId()); %> onClick="rboc(this)" class="option">
<% out.println(paper.getOptione());%>
<br>
<br>
<textarea name="<%out.println("question" + paper.getId());%>" id=<% out.println("q" + paper.getId()); %> style="display:none" onClick="clearContents(this)"><%out.println("question" + paper.getId());%></textarea>
<br>
<%
if(b!=false||x==10){
x++;
%>
<BR> </div>
<input type="button" id="next" value="NEXT" ></input>
<BR>
<%
}
}
%>
<input type='hidden' value=<%out.println(javapaperList.size());%> id="javapaperlist"/>
<input type="submit" id="submitButton" value="Submit">
</FORM>
simple example for fadein,fadeout,fadetoggle
fadein:
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
fadeout:
$("button").click(function(){
$("#div1").fadeOut();
$("#div2").fadeOut("slow");
$("#div3").fadeOut(3000);
});
fadeToggle:
$("button").click(function(){
$("#div1").fadeToggle();
$("#div2").fadeToggle("slow");
$("#div3").fadeToggle(3000);
});
On #next click you could just assign the current question to a variable and use .next()
Also, I would us .eq() instead of .get() because it prevents recalling jQuery each and every time
$(document).ready(function(){
var questions;
var counter;
var totalQuestions;
var currentQuestion=0;
var i=0;
totalQuestions=$("#javapaperlist").val();
questions=$(".questions");
questions.hide();
questions.eq(currentQuestion).fadeIn(1000);
$('#next').click(function (){
var current = questions.eq(currentQuestion);
current.stop().fadeOut(2000).next().fadeIn(1000);
currentQuestion++;
});
});
Here is a fiddle: http://jsfiddle.net/b563F/
You may fadein the next question after fadeouted the previous one,not at the same time,
and you may change your id of the "next" buttons, they all have the same id,and the click event only bind to the first one.so you may change id selector to class selector, Try this:
<input type="button" name="next" value="NEXT" class="next" >
$('.next').click(function () {
$(questions.get(currentQuestion)).stop().fadeOut(2000, function () {
currentQuestion = currentQuestion + 1;
$(questions.get(currentQuestion)).fadeIn(1000);
});
});