<ul>
<li><input type="checkbox" name="item-1" />Item 1
<ul class="sublist">
<li><input type="checkbox" name="item-1-sublist" />Item 1-1</li>
<li><input type="checkbox" name="item-1-sublist" />Item 1-2</li>
<li><input type="checkbox" name="item-1-sublist" />Item 1-3</li>
</ul>
</li>
<li>
<input type="checkbox" name="item-2" />Item 2
<ul class="sublist">
<li><input type="checkbox" name="item-2-sublist" />Item 2-1</li>
<li><input type="checkbox" name="item-2-sublist" />Item 2-2</li>
<li><input type="checkbox" name="item-2-sublist" />Item 2-3</li>
</ul>
</li>
</ul>
Considering the code above, if the user clicks on one of the main-level checkboxes (ie, Item 1 or Item 2), how would I go about toggling all checkboxes in the neighboring list with the sublist class value? I don't want to use any other class or id values because I'll have several types of sublists that need to be toggled independently; I don't want to duplicate chunks of code for each set of class/id values.
I've had the toggle-all-children-of neighbor but I've had to scrap that code.
You could use the $.parent() functions, like so (demo):
$('input[name="item-1"], input[name="item-2"]').change(function() {
var checkbox = $(this), checked = checkbox.is(':checked');
$('ul.sublist input[type="checkbox"]', checkbox.parent()).attr('checked', checked);
})
This code could be a little cleaner if your child check boxes had a name that didn't start with the same string as their parents like so:
<ul>
<li><input type="checkbox" name="item-1" />Item 1
<ul class="sublist">
<li><input type="checkbox" name="sublist-item-1-1" />Item 1-1</li>
<li><input type="checkbox" name="sublist-item-1-2" />Item 1-2</li>
<li><input type="checkbox" name="sublist-item-1-3" />Item 1-3</li>
</ul>
</li>
<li>
<input type="checkbox" name="item-2" />Item 2
<ul class="sublist">
<li><input type="checkbox" name="sublist-item-2-1" />Item 2-1</li>
<li><input type="checkbox" name="sublist-item-2-2" />Item 2-2</li>
<li><input type="checkbox" name="sublist-item-2-3" />Item 2-3</li>
</ul>
</li>
</ul>
and:
$('input[name^="item-"]').change(function() {
var checkbox = $(this), checked = checkbox.is(':checked');
$('ul.sublist input[type="checkbox"]', checkbox.parent()).attr('checked', checked);
})
This uses the pseudo selector ^= which means starts with. You could use that in your original HTML because your child selectors all start with the name string.
------Edit------
For a better result use prop instead of attr, sometimes the attr works only the first time you change the values. So it would be:
$('input[name^="item-"]').change(function() {
var checkbox = $(this), checked = checkbox.is(':checked');
$('ul.sublist input[type="checkbox"]', checkbox.parent()).prop('checked', checked);
})
Related
I have a problem reading the $_POST data after i submitted a form from a dialog.
The setup is like this:
Page1 (clientcard.php) contains a link to a dialog:
Add new contact...
Page 2 (the addcontact.php) posts the data to clientcard.php:
<h3 style="margin-top: 0px;">Create contact</h3>
<form method="POST" name="formaddcontact" id="formaddcontact" action="clientcard.php">
<input type="hidden" name="client_ref" value="<?php echo $id;?>" />
<input type="hidden" name="mode" value="addcontact" />
<ul data-role="listview" data-inset="true">
<li class="ui-field-contain">
<label for="naam">name:</label>
<input name="naam" id="naam" value="" data-clear-btn="false" type="text" />
</li>
<li class="ui-field-contain">
<label for="functie_ref">Department:</label>
<select name="functie_ref" id="functie_ref" data-native-menu="false">
<!-- some options -->
</select>
</li>
<li class="ui-field-contain">
<label for="telefoon">Phone:</label>
<input name="telefoon" id="telefoon" value="" data-clear-btn="false" type="text" />
</li>
<li class="ui-field-contain">
<label for="mobiel">Cell:</label>
<input name="mobiel" id="mobiel" value="" data-clear-btn="false" type="text" />
</li>
<li class="ui-field-contain">
<label for="email">Email:</label>
<input name="email" id="email" value="" data-clear-btn="false" type="text" />
</li>
<li class="ui-body ui-body-b">
<fieldset class="ui-grid-a">
<div class="ui-block-a">Annuleren</div>
<div class="ui-block-b">Opslaan</div>
</fieldset>
</li>
</ul>
</form>
I read in other answers that you have to use something like parent().appendTo($("form:first"));but to be honest i have NO clue where to put that...
I use jquery mobile 1.4.3 and the jquery code that is actually popping up the dialog is generated by jqm and i have no <script> code whatsoever in my files...
Any help would be greatly appreciated.
Regards, Christiaan
I am aware that you can't nest forms in html. Therefore I need help to make the following feature.
I have a list of checkboxes:
<form action="addAll.php" method="post">
<ul>
<li><input type="checkbox" name="boxes[]" value="1"> No. 1</li>
<li><input type="checkbox" name="boxes[]" value="2"> No. 2</li>
<li><input type="checkbox" name="boxes[]" value="3"> No. 3</li>
<li><input type="checkbox" name="boxes[]" value="4"> No. 4</li>
</ul>
<input type="submit" value="Add all">
</form>
When clicking the submit button "Add all", the values are gathered and sent to the addAll.php file. So far, so good.
There are links in the list, as shown. The wanted feature is now that when I click a link, the form is submitted to ANOTHER page than addAll.php.
So actually, instead of the links I need four submit buttons in place of each link, that each will submit the form to THEIR OWN file and not addAll.php.
I simply need to be able to save, which checkboxes the user has checked when he navigates between the links and back again, until he finally at some point clicks the "Add all" submit button. Therefore I want to bring the checked boxes with the user to the new page after clicking the link, so it is save and so I can re-check the checkboxes by looking at the POST data, if he returns later to click "Add all".
Is there a solution for this?
I would not recommend doing anything like this, but to me it seems to be the closest thing I can think of as a solution w/o using javascript.
addAll.php
...
function redirect($url) {
$query_string = urlencode('?boxes=['.implode(',', $_POST['boxes']).']');
$base_url = some_awkward_way_of_getting_the_base_url();
$url_full = $url_base.$url;
header('Location: '.$url_full.$query_string);
}
function alternative($url) {
$_SESSION['boxes'] = $_POST['boxes'];
$base_url = some_awkward_way_of_getting_the_base_url();
$url_full = $url_base.$url;
header('Location: '.$url_full);
}
if (isset($_POST['submit_nr1']))
redirect('nr1.php');
else if (isset($_POST['submit_nr2']))
redirect('nr2.php');
else if (isset($_POST['submit_nr3']))
redirect('nr3.php');
else if (isset($_POST['submit_nr4']))
redirect('nr4.php');
else if (isset($_POST['submit_all']))
handle_accordingly();
...
Alternatively you could have addAll.php include/require different files based on the different cases instead of doing header redirection.
checkboxes.php
...
<form action="addAll.php" method="post">
<ul>
<li>
<input type="checkbox" name="boxes[]" value="1">
<input type="submit" name="submit_nr1" value="No. 1"/>
</li>
<li>
<input type="checkbox" name="boxes[]" value="2">
<input type="submit" name="submit_nr2" value="No. 2"/>
</li>
<li>
<input type="checkbox" name="boxes[]" value="3">
<input type="submit" name="submit_nr3" value="No. 3"/>
</li>
<li>
<input type="checkbox" name="boxes[]" value="4">
<input type="submit" name="submit_nr4" value="No. 4"/>
</li>
</ul>
<input type="submit" name="submit_all" value="Add all">
</form>
...
Disclaimer: I haven't written PHP in a good while, and this is untested, but should at least illustrate a possible solution.
I want to use jQuery UI dialog box to open a form dialog where one can edit information about an employee.
The form looks like this
<form id="formAddNewRow" action="/frontend_dev.php/test/create" method="post" >
<table>
<tbody>
<ul>
<li>
<label for="employee_firstname">Firstname</label>
<input type="text" name="employee[firstname]" id="employee_firstname" />
</li>
<li>
<label for="employee_lastname">Lastname</label>
<input type="text" name="employee[lastname]" id="employee_lastname" />
</li>
<ul>
</tbody>
</table>
</form>
I want to load the form elements prefilled with the employees data. eg
<label for="employee_lastname">Lastname</label> <input type="text" name="employee[lastname]" value="Miller" id="employee_lastname" />
So my idea was to ajax a complete form that fits the selected employee and replace it with the one above.
<form id="formAddNewRow" action="/frontend_dev.php/test/create" method="post" >
<table>
<tbody>
<ul>
<li>
<label for="employee_firstname">Firstname</label>
<input type="text" name="employee[firstname]" value="Miller" id="employee_firstname" />
</li>
<li>
<label for="employee_lastname">Lastname</label>
<input type="text" name="employee[lastname]" value="Tom" id="employee_lastname" />
</li>
<ul>
</tbody>
</table>
</form>
I try doing that by
$( ".editButton" )
.button()
.click(function() {
var replace = $.ajax({
url: 'employee/edit?id=1', success: function() {
$( "#formAddNewRow" ).replaceWith(replace.responseText);
}
});
});
This works, but it stops working when I do:
$( "#formAddNewRow" ).dialog({});
There is no error message or warning. The form just gets eliminated from the DOM together with its parent node that was inserted by dialog().
How do I prefill the form succesfully?
Put your <form> into a <div> and attach the .dialog() to the div instead of to the form.
In the AJAX call replace the form as you are now, leaving its parent div attached to the dialog box.
This is necessary because jQuery UI internally maintains references to the element contained in the dialog box, and if you replace that element those references don't get updated. Replacing a child of that element will eliminate that problem.
<div id="formAddNewRowDialog">
<form id="formAddNewRow" action="/frontend_dev.php/test/create" method="post" >
<table>
<tbody>
<ul>
<li>
<label for="employee_firstname">Firstname</label>
<input type="text" name="employee[firstname]" value="Miller" id="employee_firstname" />
</li>
<li>
<label for="employee_lastname">Lastname</label>
<input type="text" name="employee[lastname]" value="Tom" id="employee_lastname" />
</li>
<ul>
</tbody>
</table>
</form>
</div>
Wrap the form in a div like above then call
$( "#formAddNewRowDialog" ).dialog();
this line:
<li><?php echo $form['genero']->renderLabel() ?></li>
is generating
<li>
<label for="usuario_genero">Genero</label>
<ul class="radio_list">
<li> <!-- this li doesn't have any id-->
<input type="radio" checked="checked" id="usuario_genero_0" value="0"
name="usuario[genero]"> <label for="usuario_genero_0">Chico</label>
</li>
<li> <!-- this li doesn't have any id-->
<input type="radio" id="usuario_genero_1" value="1"
name="usuario[genero]"> <label for="usuario_genero_1">Chica</label>
</li>
</ul>
</li>
I'd like to force the choices as inline, but the li's generated don't have the 'id' attribute.
What should i do?
Regards
Javi
In your CSS:
ul.radio_list li
{
display: inline;
}
Alternatively, you can create your own formatter for your radio widgets, or extend from the existing sfWidgetFormSelectRadio class and override the formatter() method to display the radio icons in the format you require.
Is it best to wrap forms inputs in divs:
<div id="formContainer">
<div>
<label for="username">Username</label><input type="text" id="username">
</div>
<div>
<label for="password">Password</label><input type="text" id="password">
</div>
</div>
Or is it better to wrap in ul or ol list with list-style-type set to none
<ol id="formContainer">
<li>
<label for="username">Username</label><input type="text" id="username">
</li>
<li>
<label for="password">Password</label><input type="text" id="password">
</li>
</ol>
Or does it matter at all?
I would opt with a definition list:
<dl>
<dt><label for='name'>blah</label></dt>
<dd><input type='text'></dd>
</dl>
I always use tables because that makes it so easy to align the fields.