JQuery Autopopulate
I am new to JQuery so any help will be very much appreciated
I have a group of radio buttons having same name ..
<input id="employeeType" type="radio" name="employee_type" value="Ongoing" <?php echo set_radio('employee_type', 'Ongoing', TRUE); ?>/> Ongoing
<input id="employeeType" type="radio" name="employee_type" value="Casual" <?php echo set_radio('employee_type', 'Casual'; ?>/> Casual
Based on the selection above the text feild must be auto populated .. If the user selects Casual, the text field will have to auto populate with text "N/A" otherwise the user will have to type their text in...
<input name="work_type" type="text" value="<?php echo set_value('work_type'); ?&开发者_StackOverflowgt;" size="20" maxlength="20">
Can someone please help me acheive this?
Thanks
First of all, you should give your radio boxes different ID's. ID's should always be unique.
The easiest way to achieve what you want would be to attach a click handler to your radio inputs, check the value and act accordingly:
$("[name='employee_type']").click(function() {
var val = $(this).val();
if(val == "Casual") {
$("[name='work_type']").val("N/A");
} else {
$("[name='work_type']").val("");
}
});
If user clicks "Casual", work type will contain "N/A", otherwise it will be empty.
$('[name="employee_Type"]').click(function() {
var $input = $(':input[name="work_type"]');
console.log(this.value);
if (this.value === "Casual") {
$input.val("N/A");
} else {
$input.val('');
}
});
Link to a working fiddle...
精彩评论