Here am getting offset while one of the checkbox gets unchecked,here is the checkbox list
<input type="checkbox" id="day_off_1" name="day_off[]" checked value="1" class="switch-input day_off">
<input type="checkbox" id="day_off_2" name="day_off[]" checked value="1" class="switch-input day_off">
<input type="checkbox" id="day_off_3" name="day_off[]" checked value="1" class="switch-input day_off">
Here is my controller code
$off = (!empty(Input::get('day_off'))) ? Input::get('day_off') : 0;
i tried to use isset in place of !empty but not getting allowed to use that,here if checked i want the value 1 to be stored otherwise 0
Change your HTML to have different values as you have array as below:
<input type="checkbox" id="day_off_1" name="day_off[]" checked value="1" class="switch-input day_off">
<input type="checkbox" id="day_off_2" name="day_off[]" checked value="2" class="switch-input day_off">
<input type="checkbox" id="day_off_3" name="day_off[]" checked value="3" class="switch-input day_off">
Now in controller, you will get only checked value i.e if day_off_3 checkbox is checked you will get 3 in your day_off array.
In controller, you can add code like below:
$dayOff = Input::get('day_off');
if(is_array($dayOff) && in_array(3, $dayOff)){
//do something
}
Related
I am working on an AngularJS application. I am trying to create a page that allows the user to select one of three radio buttons. Two of the three also have checkboxes underneath them to allow the user to select additional options if they've selected the appropriate radio button. To try to prevent improper checkbox selections, I'm trying to set the ng-disabled attribute on the checkboxes. So far, it's not working, and I've tried several different iterations.
This is my HTML:
<div class="panel-body">
<input type="radio" id="notFraudulent" name="actionSelector" ng-model="cleared" /><label for="notFraudulentRadio"> Not Fraudulent</label><br />
<input type="checkbox" id="highVolumeCustomer" ng-model="highVolumeCustomer" ng-disabled="(fraudulent||cleared)" /><label for="highVolumeCustomer"> High Volume Customer</label><br />
<br/>
<input type="radio" id="appearsFraudulent" name="actionSelector" ng-model="fraudulent" /><label for="isFraudulentRadio"> Appears Fraudulent</label><br />
<input type="checkbox" id="reportAccount" ng-model="reportAccount" ng-disabled="(cleared||reviewed)" /><label for="reportAccount"> Report Account</label><br />
<br/>
<input type="radio" id="markReviewed" name="actionSelector" ng-model="reviewed" /><label for="markReviewed"> Mark As Reviewed For Later</label>
</div>
I have tried changing the operator on the ng-disabled expressions to &&, as I've seen some articles where it's suggested that the operators don't mean what one thinks they mean. But that doesn't work, and neither does it work if I put just a single condition in the expression. There isn't anything in the controller (yet) that tries to use or manipulate any of the ng-models in the HTML. I've come to the conclusion that there's something I'm missing with regard to the radio buttons, but I can't for the life of me figure out what.
Can anyone see what my mistake is?
you should use value property to bind special value for radio button, and when radiobutton's status is changed, the value will be kept at ng-model.
refer the code snippet below:
angular.module("app", [])
.controller("myCtrl", function($scope) {
$scope.selectedValue = 'cleared';
$scope.cleared = false;
$scope.fraudulent = false;
$scope.reviewed = false;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="panel-body" ng-app='app' ng-controller="myCtrl">
<input type="radio" id="notFraudulentRadio" name="actionSelector" value="cleared" ng-model="selectedValue" /><label for="notFraudulentRadio"> Not Fraudulent</label><br />
<input type="checkbox" id="highVolumeCustomer" ng-model="highVolumeCustomer" ng-disabled="selectedValue === 'fraudulent' || selectedValue === 'cleared'" /><label for="highVolumeCustomer"> High Volume Customer</label><br />
<br/>
<input type="radio" id="isFraudulentRadio" name="actionSelector" value="fraudulent" ng-model="selectedValue"/><label for="isFraudulentRadio"> Appears Fraudulent</label><br />
<input type="checkbox" id="reportAccount" ng-model="reportAccount" ng-disabled="selectedValue === 'cleared' || selectedValue === 'reviewed'" /><label for="reportAccount"> Report Account</label><br />
<br/>
<input type="radio" id="markReviewed" name="actionSelector" value="reviewed" ng-model="selectedValue"/><label for="markReviewed"> Mark As Reviewed For Later</label>
<br>
cleared:{{cleared}}<br>
fraudulent:{{fraudulent}}<br>
reviewed:{{reviewed}}<br>
selectedValue: {{selectedValue}}
</div>
This is my HTML :
<input type="checkbox" name="product_image_id" value="0" />
<input type="checkbox" name="product_image_id" value="1" />
<input type="checkbox" name="product_image_id" value="2" />
If I check all the options and I use r.FormValue("product_image_id") to get the value of checked options, I will only get the value 0.
I mean I can only get the first value, and I can't get other value although it was checked.
Please help me. Thanks.
Request.FormValue only returns the first value if there is more than one. From the Documentation:
FormValue returns the first value for the named component of the query.
...
To access multiple values of the same key, call ParseForm and then
inspect Request.Form directly.
r.FormValue returns the first value from the list of options
instead use r.Form , this returns a list.
you can access your values by
r.Form["product_image_id"]
HTML
<form method="post" name="myform" action="http://localhost:8081/post" >
<input type="checkbox" name="product_image_id" value="Apple" />
<input type="checkbox" name="product_image_id" value="Orange" />
<input type="checkbox" name="product_image_id" value="Grape" />
<input type="submit">
</form>
Go
// ProcessCheckboxes will process checkboxes
func ProcessCheckboxes(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
fmt.Printf("%+v\n", r.Form)
productsSelected := r.Form["product_image_id"]
log.Println(contains(productsSelected, "Grape"))
}
func contains(slice []string, item string) bool {
set := make(map[string]struct{}, len(slice))
for _, s := range slice {
set[s] = struct{}{}
}
_, ok := set[item]
return ok
}
Output
Orange and Grape checboxes are checked and submitted
map[product_image_id:[Orange Grape]]
2016/10/27 16:16:06 true
Apple and Orange checboxes are checked and submitted
map[product_image_id:[Apple Orange]]
2016/10/27 16:17:21 false
HTML
<input type="checkbox" name="product_image_id" value="0" />
<input type="checkbox" name="product_image_id" value="1" checked/>
<input type="checkbox" name="product_image_id" value="2" checked/>
<input type="checkbox" name="product_image_id" value="3" />
<input type="checkbox" name="product_image_id" value="4" checked/>
GO
r.Form["product_image_id"][0] == "1"
r.Form["product_image_id"][1] == "2"
r.Form["product_image_id"][2] == "4"
It will be working.
Am I late?
I get data from AJAX query in Angular JS. It is information about user.
There are two input radio button in page:
<input type="radio" ng-model="sex" value="1">
<input type="radio" ng-model="sex" value="2">
From AJAX I get field {'sex' : '1'}. How I can check input with value = 1 and set it value to ng-model?
Start by assigning a name to your inputs, to make them mutually exclusive:
<input type="radio" name="sex" ng-model="sex" value="1">
<input type="radio" name="sex" ng-model="sex" value="2">
Now, ng-model="sex" means that the inputs are populated based on the value of the sex property of the scope. So all you need to check the first input, from your controller, is
$scope.sex = '1';
You can set that in your controller with your $scope.
<input type="radio" name="sex" ng-model="sex" value="1">
<input type="radio" name="sex" ng-model="sex" value="2">
$scope.sex = '1';
then you have set your variable for ng-model and you can use it.
<input id="declarationAcknowledgement" name="declarationAcknowledgement" ng-false-value="false" ng-model="formData.declarationAcknowledgement" ng-true-value="true" type="checkbox" />
<label data-localize="I Agree" for="declarationAcknowledgement" >I Agree</label>
<div class="pw-form-inline-error" data-localize="Accept to proceed" ng-show="(peopleworksForm.declarationAcknowledgement.$dirty || peopleworksForm.submited) && formData.declarationAcknowledgement != 'true'">Accept to proceed</div>
this is my view side inside my controller I set "Form.submited = true"
what I want is if the user check the check box if value is not equals to 'true' set the form.$valid false from view side not in controller how can I do this. Can any one help me?
try this.. add required="required" and make ng-false-value=""
<input required="required" name="declarationAcknowledgement" ng-false-value="" ng-model="formData.declarationAcknowledgement" ng-true-value="true" type="checkbox" />
In angular if i change my radio button from no to yes the span content should Hide.
I don't know how to take the event.
Here the fiddle
http://jsfiddle.net/Lzgqnkx9/
<div class="onoffswitch-green1">
<input type="radio" id="radios3" name="radiosgg" class="SwitchOn" value="true" checked
ng-click="radioChecked()">
<label for="radios3">Yes</label>
<input type="radio" id="radios4" name="radiosgg" class="SwitchOff" value="false" ng-
click="radiounChecked()">
<label for="radios4">No</label>
</div>
<span class="temsize">
<label>Team Size</label>
<input type="text" class="s-txtboxes">
</span>
your ans will be very helpfull .
Thanks
updated jsFiddle
Angular has an already defined behavior for handling radio inputs. You don't have to use any radioChecked function, just use combination of ng-model and value on your inputs :
<input type="radio"
ng-model="hasTeam"
value="true"/> - Yes
<input type="radio"
ng-model="hasTeam"
value="false"/> - No
<span ng-if="hasTeam">
I'm showing only if yes is selected.
</span>
Maybe you should read the docs here.
you must use ng-hide or ng-show directive
for example
HTML
<input type="radio" id="radios3" name="radiosgg" class="SwitchOn" value="true" checked
ng-click="radioChecked()" ng-model="myvar" ng-hide="hideVar">
JavaScript
$Scope.hideVar = false;
$Scope.myvar = false;
$Scope.radioChecked= function () {
$Scope.hideVar = true; // Hide your checkbox
... dosomething...
}