If checkbox unchecked Corresponding field values are posted! AngularJS - html

I have a checkbox ,if checked it has two fields (select tag ,and a textbox).
I also have a multiselect input field. Now out of checkbox and multiselect input atleast one of fields should be required. Problem here is if i Uncheck checkbox values in corresponding fields are posted.. Important checkbox value is scoped variable not to be sent to backend. Please help ..Thanks in Advance.
<div class="col-md-12">
<div class="checkbox c-checkbox">
Config.
<label><input name="checkbox" type="checkbox" checked="" value=""
ng-model="isChecked"
ng-required="!(isChecked|| trapList.length)"
/>
<span class="fa fa-check"></span></label>
</div>
<span ng-show="formStep2.$submitted && formStep2.checkbox.$invalid"
class="text-danger">This field is required</span>
</div>
<div ng-if="isChecked==true">
<div class="col-md-6" ng-if="isChecked==true">
<label class="control-label">SNMP Config.</label>
<select name="snmpConfig" id="snmpConfig"
ng-required="isChecked"
ng-model="$parent.$parent.snmpConfig"
class="chosen-select form-control">
<option value="" disabled selected>Select the SNMP COnfig.
</option>
<option value={{snmpConfig._id}} ng-repeat="snmpConfig in snmpConfigs">
{{snmpConfig.name}}
</option>
</select>
<span ng-show="formStep2.$submitted && formStep2.snmpConfig.$invalid"
class="text-danger">This field is required</span>
</div>
<div class="col-md-6" ng-if="isChecked==true">
<label class="control-label">Get Frequency
<small> (in milliseconds)</small>
</label>
<!--ng-required="protocol == 'SNMP'"-->
<input type="text" name="snmpGetFrequency" id="snmpGetFrequency"
class="form-control"
ng-required="isChecked"
onkeypress='return event.charCode >= 46 && event.charCode <= 57'
maxlength="20" ng-model="$parent.$parent.snmpGetFrequency"
ui-validate="'$value >= 1000'"/>
<span ng-show="formStep2.$submitted && formStep2.snmpGetFrequency.$invalid"
class="text-danger">Minimum required value = 1000</span>
</div>
</div>
<div class="col-md-12">
<label>Select Traps</label>
<select id="trapList" name="trapList" ng-model="$parent.$parent.trapList"
ng-required="!(isChecked||trapList.length) "
chosen="" class="form-control"
multiple="multiple">
<option value="{{trap._id}}" ng-repeat="trap in snmpTraps">{{trap.name}}
</option>
</select>
<span ng-show="formStep2.$submitted && formStep2.trapList.$invalid"
class="text-danger">This field is required</span>
<!--<span ng-show="form.validateInput('chosen', 'required')" class="text-danger">This field is required</span>-->
</div>
</div>
So if i uncheck checkbox corresponding fields should become empty or undefined, it should not be posted.

check this code , it may be help you
function Ctrl($scope) {
$scope.billing_is_shipping = true;
$scope.checked = function(){
console.log($scope.billing_is_shipping)
}
}
<div ng-app ng-controller="Ctrl">
<div ng-include src="'template.html'"></div>
</div>
<script type="text/ng-template" id="template.html">
<input type="checkbox" ng-model="$parent.billing_is_shipping" ng-change="checked()"/>
</script>

You can use ng-model with the checkbox, then use ng-disabled to listen to that model belonging to the checkboxes.
<input type="checkbox" value="Other" ng-model="myCheckboxes['other']" />
<input type="text" ng-disabled="myCheckboxes['other']" />

Related

Required is not working in <select required/>

<div class="form-group">
<div class="controls">
<label class="required" asp-for="Designation"></label> <span class="danger" asp-validation-for="Designation"></span>
<select id="Designation" name="Designation" class="form-control" asp-for="Designation" required>
<option value="0" selected>Please select a Designation</option>
#foreach (var item in designation list)
{<option value="#item.DesignationName">#item.DesignationName</option>
}
</select></div>
this is my html drop down where designation list fetch the data of all employee which works fine
MY PROBLEM is that the I put required in select statement for validation and which doesn't work as it do not shows any message when field is not filled .How can I solve this
I need when No designation is selected it shows field required message on form
Try using an empty value in the first tag, since required works on empty elements. You may refer to the documentation if needed.
<div class="form-group">
<div class="controls">
<label class="required" asp-for="Designation"></label>
<span class="danger" asp-validation-for="Designation"></span>
<select id="Designation" name="Designation" class="form-control" asp-for="Designation" required>
<option value="" selected>Please select a Designation</option>
#foreach (var item in designation list)
{
<option value="#item.DesignationName">#item.DesignationName</option>
}
</select>
</div>
</div>

Enable/Disable dropdown on radio button select

I have modal popup window which open on button click for each row in data table. On modal window I have 4 dropdowns and out of these I want one dropdown to be disabled for every row data. When user selects the radio button value as "Yes" it should be enabled.
Here is my script:
$(document).ready(function(){
$('.action').attr('disabled', 'disabled');
var $checkBox = $('.check');
$checkBox.on('change', function(e) {
//get the previous element to us, which is the select
var $select = $(this).prev();
if (this.checked) {
$select.removeAttr('disabled');
} else {
$select.attr('disabled', 'disabled');
}
});
});
This is my html:
<div class="form-group">
<label>Action Taken:</label>
<input type="radio" class="check" id="check" name="check" value="Yes">Yes
<input type="radio" class="check" id="check" name="check" value="Yes">No
<select name="action" id="action" class="form-control" disabled>
<option value="">Select Action</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</div>
If I am adding the radio buttons after select tag its's working for one row, but after submit when I am selecting second row data it stops working then I have to refresh page again, but I want radio button selection before the select tag also it should work for each row. Can someone help me out what I am doing wrong here
first of all you need 2 different values for your radio button , you have Yes for both buttons
<input type="radio" class="check" name="check" value="Yes">Yes
<input type="radio" class="check" name="check" value="No">No
you can put your radio buttons and select in a parent div and use that parent to get to access to selectbox instead of using next , prev ... like <div class="form-group">
$('.check').change(function(){
let select = $(this).parents('.form-group:first').find('select') ;
if( $(this).val() == 'Yes')
select.attr('disabled' , false );
else
select.attr('disabled' , true );
})
https://jsfiddle.net/ns5g3rqe/
There's several issues here, and each need to be corrected for your code to work properly:
The 'No' checkbox has a value of 'Yes'. This needs to be changed in order for your JS to determine which box has been checked.
.action is a class selector, yet the select has that id. As your question suggests there may be multiple clones of this HTML, remove the id on the select and use the class instead
The select is not the prev() element to either radio. Fix this by retrieving the closest() common parent and find() from there.
You're repeated the same id of #check on the radio buttons, when they must be unique. Either change or remove them.
With that corrected, the code works:
jQuery($ => {
$('.action').prop('disabled', true);
let $checkBox = $('.check').on('change', e => {
var $select = $(e.target).closest('.form-group').find('.action');
$select.prop('disabled', e.target.value !== 'Yes' && e.target.checked);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
<label>Action Taken:</label>
<label>
<input type="radio" class="check" name="check" value="Yes" />
Yes
</label>
<label>
<input type="radio" class="check" name="check" value="No" checked />
No
</label>
<select name="action" class="action form-control" disabled>
<option value="">Select Action</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</div>
<div class="form-group">
<label>Action Taken:</label>
<label>
<input type="radio" class="check" name="check" value="Yes" />
Yes
</label>
<label>
<input type="radio" class="check" name="check" value="No" checked />
No
</label>
<select name="action" class="action form-control" disabled>
<option value="">Select Action</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</div>

Angular 6 and bootstrap 4: date picker

I have a form which contain a date picker / calender, email, city name and hotel name, I want a user to select a date and enter other fields and submit the data.
This is what I have.
HTML:
<form [formGroup]="angForm" class="form-element">
<div class="form-group">
<label for="dateOfBirth">Date of Birth</label>
<input id="dateOfBirth" name="dateOfBirth" [(ngModel)]="dateOfBirth" type="text" bsDatepicker class="form-control" />
</div>
<div class="form-group form-element_email">
<input type="email" class="form-control info" placeholder="Email" formControlName="email" #email (ngModelChange)="onChange($event)">
</div>
<div *ngIf="angForm.controls['email'].invalid && (angForm.controls['email'].dirty || angForm.controls['email'].touched)"
class="alert alert-danger">
<div *ngIf="angForm.controls['email'].errors.required">
Email is required.
</div>
</div>
<div class="input-group mb-3 form-element_city">
<select class="custom-select" id="inputGroupSelect01" #cityName>
<option selected *ngFor="let city of cities" [ngValue]="city.name">{{city.name}}</option>
</select>
</div>
<div class="input-group mb-3 form-element_hotel">
<select class="custom-select" id="inputGroupSelect01" #hotelName>
<option selected *ngFor="let hotel of hotels" [ngValue]="hotel.name">{{hotel.name}}</option>
</select>
</div>
<div class="form-group">
<button type="submit" (click)="addReview(email.value, cityName.value , hotelName.value)" class="btn btn-primary btn-block form-element_btn"
[disabled]="!validEmail">Book</button>
</div>
</form>
This dispalys the following result: wrong one
Instead of that input with date picker , I just want the full calender to be display on the left side ,
Here is what I want . Good one
I tried many solution online I could not be able to solve my problem,
What do I need to change in my code to get the result I want? please Am newbie though, thanks
You can actually provide placement in the element which uses bdDatepicker directive.
Just set this placement property to left value.
Here is example in documentation.
https://valor-software.com/ngx-bootstrap/#/datepicker#placement

Listbox values not being stored to the database in Classic ASP

I am working on a Classic ASP page.
Earlier there was a checkbox list in the page which I have changed to a multi select listbox. Now after I changed it to multi select listbox the selected values are not getting saved into the database which was saving when it was a checkbox list.
The below is code from my html page....
<div class="fields options secondary-category">
<legend>Secondary Category</legend>
<p class="description">You may select up to two secondary categories in which you would like to be considered for an award (please hold the control key to select two categories).</p>
<select id="AwardsSecondaryCatagory" class="input-listbox" type="listbox" name="AwardsSecondaryCatagory" multiple="multiple" size="5">
<option>Select an option</option>
<option name="financialexcellence" value="Financial Excellence">Financial Excellence</option>
<option name="operationalexcellence" value="Operational Excellence">Operational Excellence</option>
<option name="employeeexcellence" value="Employee Excellence">Employee Excellence</option>
<option name="customerexcellence" value="Customer Excellence">Customer Excellence</option>
<option name="Innovation" value="Innovation">Innovation</option>
<option name="Transformation" value="Transformation">Transformation</option>
</select>
<!--
<div class="field opt">
<input id="financialexcellence" class="input-checkbox" type="checkbox" value="Financial Excellence" name="financialexcellence">
<label for="financialexcellence">Financial Excellence</label>
</div>
<div class="field opt">
<input id="operationalexcellence" class="input-checkbox" type="checkbox" value="Opertional Excellence" name="operationalexcellence">
<label for="operationalexcellence">Operational Excellence</label>
</div>
<div class="field opt">
<input id="employeeexcellence" class="input-checkbox" type="checkbox" value="Employee Execellence" name="employeeexcellence">
<label for="employeeexcellence">Employee Excellence</label>
</div>
<div class="field opt">
<input id="customerexcellence" class="input-checkbox" type="checkbox" value="Customer Excellence" name="customerexcellence">
<label for="customerexcellence">Customer Excellence</label>
</div>
<div class="field opt">
<input id="Innovation" class="input-checkbox" type="checkbox" value="Innovation" name="Innovation">
<label for="Innovation">Innovation</label>
</div>
<div class="field opt">
<input id="Transformation" class="input-checkbox" type="checkbox" value="Transformation" name="Transformation">
<label for="Transformation">Transformation</label>
</div>
-->
Selectbox values are submitted with the name of the select element.
The name attribute on the option is not used (is invalid to be accurate), so all selected values are submitted as AwardsSecondaryCatagory
You can recover you data just by:
<%
AwardsSecondaryCatagory = request("AwardsSecondaryCatagory")
%>
and, then, just store it in your database!

"checked" and "selected" in HTML for input do not work with AngularJS

I am learning AngularJS and user input. In my code, I tried to set default state for drop down menu and radio button with "selected" and "checked".
However, they do not work together with "ng-model" attribute.
Also, for the first radio button (ascending), the empty value attribute seems to hinder with "checked" attribute.
Could anyone explain why this happens and how to bypass this problem?
<div class="search">
<h1>Artist Directory</h1>
<label> Search: </label>
<input ng-model="query" placeholder="Search for artists" autofocus>
<label class="formgroup">by:
<select ng-model="listOrder" name="direction">
<option value ="name" selected="selected"> Name</option>
<option value="reknown"> Reknown</option>
</select>
</label>
<label class="formgroup">
<input ng-model="direction" type="radio" value=" " checked> Ascending
</label>
<label class="formgroup">
<input ng-model="direction" type="radio" value="reverse"> Descending
</label>
</div>
tie your button and select to an ng-model. Default values are the values you put in the model when the scope is first created. Then values are updated when user clicks :
function test($scope) {
$scope.selected = ['1','3'];
$scope.checked = "green";
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
<div ng-app>
<h2>Todo</h2>
<div ng-controller="test">
<select name="" ng-model="selected" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<br><br>
<input type="radio" ng-model="checked" value="red"> Red <br/>
<input type="radio" ng-model="checked" value="green"> Green <br/>
<input type="radio" ng-model="checked" value="blue"> Blue <br/>
<br>
Selected : {{selected}}
<br>
Checked : {{checked}}
</div>
</div>
See it in action : http://jsfiddle.net/913n30zf/
Select in AngularJS looks likes this:
<select ng-model="selectedOpt"
ng-options="opt for opt in listOrder">
</select>
And back in the controller you set
$scope.selectedOpt = $scope.listOrder[0];
which option you want to be select by default.