I work on asp.net mvc I face issue i can't store value of health status on hidden field using jQuery then pass hidden field value after get value to approve () function when click agree button click on hidden field i will assign value true when checkbox Yes checked and assign value false when check box No checked then after hidden field get value true or false i will pass it to approve function java script when click button agree i'm not use hidden field before so can you help me please
@Html.Label("HealthStatus?", htmlAttributes: new { @class = "control-label col-md-5" })
Yes
No
agreeon csharp property HealthStatus is boolean as below
public class HealthStatusData
{
public bool? HealthStatus {get;set;}
}so how to create hidden field then assign value of Healthstatus to hidden field using jQuery
$('.healthstatus-checkbox').on('change', function () {
// how to assign value to hidden field
});then after that pass to approve function java script
function approve() {
var HealthStatusData = new Object();
HealthStatusData.HealthStatus = ???????????????
// how to get hidden field value here
}
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 22, 2024, 12:23 AM EST
You can create a hidden field in your HTML and then use jQuery to update the value of the hidden field based on the checkbox selection. Here’s how you can do it:
First, add a hidden field to your HTML:
Then, update your jQuery code to set the value of the hidden field when the checkbox value changes:
$('.healthstatus-checkbox').on('change', function () { var healthStatusValue = $(this).val() === 'true'; $('#hiddenHealthStatus').val(healthStatusValue); });Finally, in your
approvefunction, you can get the value of the hidden field like this:function approve() { var HealthStatusData = new Object(); HealthStatusData.HealthStatus = $('#hiddenHealthStatus').val() === 'true'; // Continue with your code... }This code will set the
HealthStatusproperty ofHealthStatusDatatotrueif the hidden field value is ‘true’, andfalseotherwise. Please note that the hidden field stores its value as a string, so we need to compare it with the string ‘true’ to get a boolean value. IThanks