I'm trying to get a form to output a number value based on the outcome of two fields. A value input and a radio selection but I can't seem to get it to work.
<form oninput="retVal.value=parseInt(pavVal.value)-(parseInt(pavVal.value)*parseInt(pavCAT.value))">
<table>
<tr>
<td>
<label for="pavVal"><b>PAV:</b></label>
</td>
<td>
<input type="number" id="pavVal">
</td>
</tr>
<tr>
<td>
<label for="pavCAT" ><b>CAT:</b></label>
<td>
<input type="checkbox" name="pavCAT" id="pavCAT" value="0.3">
<label for="0.3" > N (30%)</label>
<input type="checkbox" name="pavCAT" id="pavCAT" value="0.2">
<label for="0.2" > S (20%)</label>
<input type="checkbox" name="pavCAT" id="pavCAT" value="0.1">
<label for="0.1"> B (10%)</label>
</td>
</tr>
<tr>
<td>
<label for="retVal"><b>Retention Value:</b></label>
</td>
<td>
<output name="retVal" for="pavVal pavCAT"></output>
</td>
</tr>
</table>
</form>
Anyone able to tell me where I'm going wrong>
Ids must be unique -- there were three #pavCAT. When the browser is told to do anything with #pavCAT, it will find the first #pavCAT do whatever it was supposed to do then quit, leaving the other 2 #pavCATs untouched as if they never existed. The easiest solution is to suffix each id with a number.
The rest of the code should work since in this case the id #pavCAT is just as accessible as the name [name="pavCAT"].
Also, the HTML had checkmarks not radios.
The inline event handler is now streamlined:
oninput = "retVal.value = pavVal.value - (pavVal.value * pavCAT.value)"
Note that parseInt() isn't used to convert the string values into numbers. That's because the strings are being coerced into numbers by the use of these operators: - and *.
<form id='calc' oninput = "retVal.value = pavVal.value - (pavVal.value * pavCAT.value)">
<table>
<tr>
<td>
<label for="pavVal"><b>PAV:</b></label>
</td>
<td>
<input id="pavVal" type="number">
</td>
</tr>
<tr>
<td>
<label for="pavCAT"><b>CAT:</b></label>
</td>
<td>
<input id="pavCAT3" name="pavCAT" type="radio" value="0.3">
<label for="pavCAT3"> N (30%)</label>
<input id="pavCAT2" name="pavCAT" type="radio" value="0.2">
<label for="pavCAT2"> S (20%)</label>
<input id="pavCAT1" name="pavCAT" type="radio" value="0.1">
<label for="pavCAT1"> B (10%)</label>
</td>
</tr>
<tr>
<td>
<label for="retVal"><b>Retention Value:</b></label>
</td>
<td>
<output name="retVal" for="pavVal pavCAT"></output>
</td>
</tr>
</table>
</form>
Your formula is working, but you assigned the same ID for the pavCat checkbox elements, thus returning NaN and not the correctly assigned value - try it yourself with removing/commenting out two ot the pavCAT elements.
imho is a checkbox the wrong UI element - go for a single select dropdown or even better a radio input.
Also parseInt should be parseFloat instead for pavCat. Anyways it's a little bit over the top in this situation and you could totally remove it in this simple example - maybe you have more context to justify parsing it.
<input type="radio" id="pavCat1" name="pavCAT" value="0.3">
<label for="0.3" > N (30%)</label>
<input type="radio" id="pavCat2" name="pavCAT" value="0.2">
<label for="0.2" > S (20%)</label>
<input type="radio" id="pavCat3" name="pavCAT" value="0.1">
<label for="0.1"> B (10%)</label>
https://jsfiddle.net/p9v1f53w/16/
Related
Basically what I'm looking to do is the following:
Have an HTML table in my jsp. Each row corresponds to a specific part. In one of the columns I would like to have a radio button that corresponds to an action for that specific part Let's say 'Delete', 'Archive', 'Ignore'. I'd like to pass this information on to my servlet.
Ideally I'd like to parse the entire table, for each part ID seeing whether the user selected 'Delete', 'Archive' or 'Ignore' then perform an action based on that. I have two questions:
First, What is the best way/is it possible to access the entire table from the request?
Second, How can I access the radio buttons for a specific row (part id) and determine which one was selected?
<form action="">
<table>
<tr>
<th>Row ID</th>
<th>Radio</th>
</tr>
<tr>
<td>12341</td>
<td>
<label class="radio-inline"><input type="radio" name="12341_radio" value="1">Delete</label>
<label class="radio-inline"><input type="radio" name="12341_radio" value="2">Archive</label>
<label class="radio-inline"><input type="radio" name="12341_radio" value="3">Ignore</label>
</td>
</tr>
<tr>
<td>12342</td>
<td>
<label class="radio-inline"><input type="radio" name="12342_radio" value="1">Delete</label>
<label class="radio-inline"><input type="radio" name="12342_radio" value="2">Archive</label>
<label class="radio-inline"><input type="radio" name="12342_radio" value="3">Ignore</label>
</td>
</tr>
<tr>
<td>12343</td>
<td>
<label class="radio-inline"><input type="radio" name="12343_radio" value="1">Delete</label>
<label class="radio-inline"><input type="radio" name="12343_radio" value="2">Archive</label>
<label class="radio-inline"><input type="radio" name="12343_radio" value="3">Ignore</label>
</td>
</tr>
<tr>
<td>12344</td>
<td>
<label class="radio-inline"><input type="radio" name="12344_radio" value="1">Delete</label>
<label class="radio-inline"><input type="radio" name="12344_radio" value="2">Archive</label>
<label class="radio-inline"><input type="radio" name="12344_radio" value="3">Ignore</label>
</td>
</tr>
<tr>
<td>12345</td>
<td>
<label class="radio-inline"><input type="radio" name="12345_radio" value="1">Delete</label>
<label class="radio-inline"><input type="radio" name="12345_radio" value="2">Archive</label>
<label class="radio-inline"><input type="radio" name="12345_radio" value="3">Ignore</label>
</td>
</tr>
</table>
<input type="hidden" name="id_list" val="12341,12342,12343,12344,12345">
</form>
In above code i have put common name for radio in same row. And every group name contains rowid following with _radio and an hidden input containing comma separated list of row ids.
On submitting form on servlet side you should perform operations listed below :-
First of all get the hidden input.
Separate then into an array.
Iterate over array so you have id list and form string rowid_radio i.e. '12341_radio','12342_radio' etc.
then you can get values of all the radio buttons from different row.
I need to create 210 checkboxes each with unique names, id's and label for values.
The checkboxes reside in tables, where there are 6 checkboxes to a table.
I've manually done this for 54 checkboxes (9 tables), and want to automate the creation of the last 156 checkboxes (26 tables).
I therefore want to automate the incrementation of:
name
id
label for value
This is the simplest way I can see to do it:
Create the base structure for 26 tables (see below).
Use the Text Pastry plugin for Sublime Text.
Use a variant of this example:
https://github.com/duydao/Text-Pastry/wiki/Examples#insert-nums-syntax
With the instructions to:
Find "checkbox00"
Replace with "checkbox55"
Do this for 3 instances
Increment the number used in the replacement text and repeat for the remaining instances.
Is this the sort of thing that Text Pastry was designed to do?
If so, and it's not too large a task to explain, specific directions would be appreciated.
<!-- BEGIN table 10 -->
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkbox00" id="checkbox00" class="css-checkbox" />
<label for="checkbox00" class="css-label">custom_text_A</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkbox00" id="checkbox00" class="css-checkbox"/>
<label for="checkbox00" class="css-label">custom_text_B</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkbox00" id="checkbox00" class="css-checkbox" />
<label for="checkbox00" class="css-label">custom_text_C</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkbox00" id="checkbox00" class="css-checkbox" />
<label for="checkbox00" class="css-label">custom_text_D</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkbox00" id="checkbox00" class="css-checkbox"/>
<label for="checkbox00" class="css-label">custom_text_E</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkbox00" id="checkbox00" class="css-checkbox" />
<label for="checkbox00" class="css-label">custom_text_F</label>
</td>
</tr>
<!-- END table 10 -->
<!-- BEGIN table 11 -->
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkbox00" id="checkbox00" class="css-checkbox" />
<label for="checkbox00" class="css-label">custom_text_A</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkbox00" id="checkbox00" class="css-checkbox"/>
<label for="checkbox00" class="css-label">custom_text_B</label>
</td>
</tr>
<!-- ... etc, continue for 26 tables -->
Update/Solution
Here's a Python script I put together quickly that seems to do what I want it to do:
my_string = "<tr><td class=\"area_checkbox\"> \
<input type=\"checkbox\" name=\"checkbox00\" id=\"checkbox00\" class=\"css-checkbox\" /> \
<label for=\"checkbox00\" class=\"css-label\">custom_text_00</label> \
</td></tr>"
growing_string = ""
cb_suffix = 55
text_suffix = 1
for i in range(156):
if text_suffix <= 6:
temp_string = my_string.replace("checkbox00", "checkbox" + str(cb_suffix),3)
temp_string_2 = temp_string.replace("custom_text_00", "custom_text_0" + str(text_suffix))
growing_string += temp_string_2
cb_suffix += 1
text_suffix += 1
else:
text_suffix = 1
temp_string = my_string.replace("checkbox00", "checkbox" + str(cb_suffix),3)
temp_string_2 = temp_string.replace("custom_text_00", "custom_text_0" + str(text_suffix))
growing_string += temp_string_2
cb_suffix += 1
text_suffix += 1
print growing_string
Then I copied the output to JSFiddle and clicked TidyUp to format it :).
Sample Output:
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkbox55" id="checkbox55" class="css-checkbox" />
<label for="checkbox55" class="css-label">custom_text_01</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkbox56" id="checkbox56" class="css-checkbox" />
<label for="checkbox56" class="css-label">custom_text_02</label>
</td>
</tr>
I've following form
<form method="post" action="getParameterValuesServlet">
<table border="1">
<tr>
<td>User Name</td>
<td><input type="text" name="userName"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>Gender</td>
<td>
<input type="radio" name="gender" value="male">Male<br>
<input type="radio" name="gender" value="female">Female<br>
</td>
</tr>
<tr>
<td>Hobbies</td>
<td>
<input type="checkbox" name="hobbies" value="cycling">Cycling<br>
<input type="checkbox" name="hobbies" value="swimming">Swimming<br>
<input type="checkbox" name="hobbies" value="treking">Treking<br>
</td>
</tr>
<tr>
<td>City</td>
<td>
<select name="city" multiple>
<option value="mysore">Mysore</option>
<option value="pune">Pune</option>
<option value="chandigarh">Chandigarh</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Register"></td>
</tr>
</table>
</form>
When I submit this form without entering or selecting any values, I see only blank userName and password (text & password) in post body (same is the case with GET query string)
Why don't radio button, check box and drop down values get submitted with default values?
One more thing, if I remove "multiple" attribute from "select" tag, the first option gets submitted.
Would be thankful, if someone explains this behavior!!
FYI, I'm using Fiddler Web Debugger tool to trace the request and response and submitting this form to a Java Servlet
What default values do you imagine would be sent?
The only values that get sent are the selected options; since no options are selected, there's nothing to send.
That's in contrast to text and password controls, which always have a value, even if that value is the empty string.
You can use a hidden field with a fallback value, if a checkbox in a form is submitted unchecked:
<input type="hidden" name="hobbies" value="">
<input type="checkbox" name="hobbies" value="swimming">Swimming<br>
This seems to work also with radio buttons:
<input type="hidden" name="gender" value="">
<input type="radio" name="gender" value="male">Male<br>
<input type="radio" name="gender" value="female">Female<br>
I have done something like this.
<html>
<head>
<script type="text/javascript">
<!--
function toggleTB(what){
if(what.checked){document.theForm.theTB.disabled=1}
else{document.theForm.theTB.disabled=0}}
//-->
</script>
</head>
<body>
<form name="theForm">
<input type="checkbox" name="theCB" onClick="toggleTB(this)">Toggle The Text Box<br>
<input type="text" name="theTB" value="asdf">
</form>
</body>
</html>
But this is only used for one time.i need this function repeatedly in another rows also so how can i used this function for multiple times.
My form goes like this:
<tr>
<td style="border-top:none; text-decoration:underline;" >Specific operations/procedures</td>
<td>
<input type="checkbox" name="sd3[]" value="mfi_nam9" />Other(please specify):
<input type="text" name="mfi_nam9" class="text required" id="mfi_name"
</td>
</tr>
<tr>
<td style="border-top:none; text-decoration:underline;" >General principles/strategies</td>
<td>
<input type="checkbox" name="sd2[]" value="mfi_nam8" />Other(please specify):
<input type="text" name="mfi_nam8" class="text required" id="mfi_name"
</td>
</tr>
i will be waiting for ur response and i am very thankful to u guys for helping me previously and hope u will help me this time too.
Read this article
i would prefer jQuery
Here is DEMO
Another DEMO
We can take the code and do the modifications like
1. Javascript modifications :
function toggleTB(what,elid)
{
if(what.checked)
{
document.getElementById(elid).disabled=1
}
else
{
document.getElementById(elid).disabled=0
}
}
2. Checkbox HTML code modifications
<input type="checkbox" name="sd3[]" value="mfi_nam9" onClick="toggleTB(this,'mfi_name1')" />Other(please specify):
<input type="text" name="mfi_nam9" class="text required" id="mfi_name1" />
Note that we have used the ID's to be varying for each of the textboxes which can be generated even when you are generating these textboxes from the php codes.
Add onclick to each of the checkbox
<input type="checkbox" name="sd2[]" value="mfi_nam8" onClick="toggleTB(this)" />Other(please specify):
and declare toggleTB as
function toggleTB(what){
what.form.elements[what.value].disabled = what.checked;
}
Java Script modification :
function toggleTB(what){
var theTB = document.getElementById(what.value);
if(what.checked){theTB.disabled=1}
else{theTB.disabled=0}
}
HTML Modification :
<table>
<tr>
<td style="border-top:none; text-decoration:underline;" >Specific operations/procedures</td>
<td>
<input type="checkbox" name="sd3[]" onClick="toggleTB(this)" value="mfi_nam9" />Other(please specify):
<input type="text" name="mfi_nam9" id="mfi_nam9" class="text required" />
</td>
</tr>
<tr>
<td style="border-top:none; text-decoration:underline;" >General principles/strategies</td>
<td>
<input type="checkbox" name="sd2[]" onClick="toggleTB(this)" value="mfi_nam8" />Other(please specify):
<input type="text" name="mfi_nam8" id="mfi_nam8" class="text required" />
</td>
</tr>
</table>
Note: Here I have used ID rather than NAME to verify the form input box element.
I think this doesn't make sense to disable the TEXT BOX on checked event of the related CHECK BOX. You maybe want to enable the TEXT BOX whenever some one checked the check box to specify some other thing, I am not sure what you want to do with this.
If you want to do like what I guess, just change the JAVA SCRIPT lines as bellow -
if(what.checked){theTB.disabled=0} // have placed 0 in place of 1
else{theTB.disabled=1} // have placed 1 in place of 0
}
HTML INPUT-BOX as bellow -
OR if you think to toggle (enable/disable) the checkbox, this is not possible cause you know after disable an element the click event will not do action on the element so how it will be disable :)
i have this code where you can select the date you like to attend the meeting.
How can I have this possible to select 2 dates at the same time?
Because in this code, one can only select 1 date. What should I add in here?
<div style="background-color:#aaa">
<form method="post" action="[~[*id*]~]">
<input type="hidden" name="formid" value="registrationForm" />
<p>
<table>
<tr>
<td><label for="workshop" style="margin:0.5em">Termine:</label>
<td>
<input type="checkbox" name="termine1" value="Montag 4. Oktober 2010" eform="Termine::1"/> Montag 4. Oktober 2010 <br/>
<input type="checkbox" name="termine2" value="Mittwoch 13. Oktober 2010" /> Mittwoch 13. Oktober 2010 <br/>
<input type="checkbox" name="termine3" value="Freitag 22. Oktober 2010" /> Freitag 22. Oktober 2010 <br/>
</td>
</tr>
<tr>
<td><label for="email" style="margin:0.5em">Email:</label></td>
<td><input type="text" name="email" size="60" maxlength="60" eform="Email:email:1" /><td>
</tr>
<tr>
<td><label style="margin:0.5em; display:block" for="kopieren" >Bitte kopieren Sie den Anti-Spam Code ein: </label>
<img src="[+verimageurl+]" alt="verification code" border="1" style="margin:0.5em"/></td>
<td valign="top"><input type="text" name="vericode" size="20" />
</tr>
<tr>
<td rowspan="3" valign="right">
<input align="right" type="submit" name="submit" style="margin:0.5em" value="Register" />
</td>
</tr>
</table>
=========
This would be the other form look like, do I need to add here for the other dates?
<h3>Registration</h3>
<table>
<tr valign="top"><td>Termine</td><td>[+termine1+]</td></tr>
<tr valign="top"><td>Termine</td><td>[+termine2+]</td></tr>
<tr valign="top"><td>Termine</td><td>[+termine3+]</td></tr>
<tr valign="top"><td>Email</td><td>[+email+]</td></tr>
</table>
Make them Checkboxes instead?
<input type="checkbox" name="workshop1" value="morning-with-visit" eform="Selected Dates::1"/> 18 August 2010<br/>
<input type="checkbox" name="workshop2" value="morning-without-visit" /> 19 August 2010<br/>
<input type="checkbox" name="workshop3" value="afternoon-with-visit" /> 20 August 2010 (12h50)<br/>
Try it out here.
Change the inputs to type="checkbox". You will likely need to change the name attribute to correctly process server-side, assuming that's what you're doing.
I agree with the three answers supplied that you should use checkboxes, thus:
<input type="checkbox" name="morning-with-visit" eform="Selected Dates::1"/> 18 August 2010<br/>
<input type="radio" name="morning-without-visit" /> 19 August 2010<br/>
<input type="radio" name="afternoon-with-visit" /> 20 August 2010 (12h50)<br/>
On the server-side, you will only receive back the boxes that were ticked. Here is a PHP example...
if (isset($_POST['morning-with-visit'])) {
echo 'They selected "morning-with-visit"';
}
You can make radio inputs multi-selectable, but by putting each in its own name... like this example (I only mention it to say that is IS possible).
<input type="radio" name="workshop1" value="morning-with-visit" eform="Selected Dates::1"/> 18 August 2010<br/>
<input type="radio" name="workshop2" value="morning-without-visit" /> 19 August 2010<br/>
<input type="radio" name="workshop3" value="afternoon-with-visit" /> 20 August 2010 (12h50)<br/>
Note that if you do this, you can't actually "un-select" anything, which is bad. So many people use yes-no groups (although checkboxes are more natural for this).
You can't have multiple selections in a radio group. You need to use something like chekboxes and perform the validation yourself, to make sure that 2 values are selected. This can be done using javascript to get client-side validation before posting the values to the server.