Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
This code is work, when I choose option 1 which is 'INDIVIDUAL', it change value of input to 'staff name', but when I choose option 2 which is 'COMPANY', the value in input did not change. How to solve it?

What I have tried:

HTML
<div class="form-group">
	<label class="col-sm-2 control-label bold	 text-right">Pay Method *</label>
	<div class="col-sm-4">
		<div class="mt-radio-list">
			<?php
				foreach ($ListPayMethod as $key => $value){
					$data['pay_method'] == $key ? $xSelect = ' checked="checked"' : $xSelect = '';
					echo '<label class="mt-radio mt-radio-outline form-control-static" id="pay_as">
					<input type="radio" name="pay_method" id="pay_method" value="'.$key.'" '.$xSelect.'> '.$value.'</label>  ';
					}
			?>
			</div>
		</div>
	</div>

<div class="form-group" id="">
	<label class="col-sm-2 control-label bold text-right">Company Name <p class="font-red">(Own Company Name)</p></label>
	<div class="col-sm-7">
		<input type="text" name="company_name" id="company_name" class="company_name form-control" placeholder="Company Name" value="<?php echo $data['company_name']; ?>">
	</div>
</div>

<div class="form-group" id="staff_name">
	<label class="col-sm-2 control-label bold text-right">Staff Name <p class="font-red">(As per IC)</p></label>
<div class="col-sm-7">
	<input type="text" name="individual_name" id="individual_name"  class="individual_name form-control uppercase" placeholder="Individual Name (AS PER IC)" value="<?php echo $data['individual_name']; ?>">
	</div>
</div>

<div class="form-group">
	<label class="col-sm-2 control-label bold text-right">Bank Name </label>
	<div class="col-sm-7">
		<input type="text" name="bank_account_name" class="bank_account_name form-control uppercase" placeholder="Bank Account Name" value="<?php echo $data['bank_account_name']; ?>">
	</div>
</div>

<div class="form-group">
		<label class="col-sm-2 control-label bold text-right">Bank Holder Name </label>
	<div class="col-sm-7">
		<input type="text" name="bank_holder_name" class="bank_holder_name form-control uppercase" placeholder="Bank Holder Name" value="<?php echo $data['bank_holder_name']; ?>">
</div>
</div>


$("#pay_method").change(function(e){
 e.preventDefault(); 
 var pay_as= $("input:radio[name=pay_method]:checked").val();
 //var pay_as = $('#pay_method').val();
 var staff_name = $('#fullname').val();
			
 if (pay_as == "1") {
	//$('.company_name').val(staff_name); //not show value
	  $('.individual_name').val(staff_name);
	  $('.bank_holder_name').val(staff_name);
 }
 else {
	//$('.individual_name').val(staff_name); //not show value
	  $('.company_name').val(staff_name);
	  $('.bank_holder_name').val(staff_name);
 }
});
Posted
Updated 21-Dec-22 16:52pm
v2
Comments
Richard Deeming 22-Dec-22 4:14am    
You haven't explained which input is not updating.

If it's the "Bank name" input, both branches of your if statement set it to the same value, so you won't notice any difference.

If it's the "Company Name" or "Staff Name" inputs, your code never clears them; it only sets one or the other to the staff_name, depending on the payment method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900