I am wondering if there is a way to use JavaScript to check all form fields with a similar name for a specific value.
I have several hidden fields on my form with names like "associd1", "associd2" etc..
Is there a way to loop through all fields with a name like "associd" and check the value of those fields for a certain value?
Of course there is. Get them with document.querySelectorAll or jQuery and set their checked property. Give it a try and come back with your code if you have questions or problems.
Related
I have a form I created in pdf that basically has a dropdown with the student's names that uses their Student ID's as the values. Pretty straightforward. I am able to populate another field (textbox) with the value of the selected item in the dropdown. Works great. I have searched in whatever ways I know how, but I am unable to find an answer to this question:
I have a second dropdown that I very simply want to populate with the same selected index as the first. In other words the selected indexes of both dropdowns will always match each other (I don't need a two way function, I just want the second dropdown to match the first.). I don't know of a way to assign two values to each dropdown item or I would try that. In theory, this problem seems like it should be really simple to solve, but I guess that's what makes me, me.
Here is the simple code I use to get the value from the dropdown and populate the textbox:
event.value = this.getField("Fieldname").value;
Thank you.
After much experimentation, I finally realized there actually IS a simple way to do this. Let me try to explain. The first dropdown has student names using their student ID's as the VALUES. The ID text box pulls the values from the selected name to display the id number. I needed the third dropdown field to display the address. Here's how simple that was:
I called the value from the textbox (which was the student ID). I set up the third dropbdown to have the address as the display and the Student ID as the VALUE of the address. I then just set it up like the iroginal:
event.value = this.getField("Fieldname").value;
Voila! It now displays the address in the third field.
It seems like you can actually daisy chain this method repeatedly to autofill even more fields if you needed to.
I set the input value Array property sum and it shows value in input but when submitting form does not get Quantity property in Order object. If I change the value then I get Quantity property value. How can I get model value from ng-value?
<input ng-model="Order.Quantity" ng-value="subOrderList.sum('Quantity')" type="number">
To answer your question, "ng-value does not update Ng-model", it is by design. As mentioned in comments, ng-value and ng-model are not intended to by used in this way.
It isn't entirely clear what you are trying to achieve here, so here's a couple potential solutions:
If you are just looking to display a value then you don't need to use an input at all. Both of these will behave the same and update when needed:
<span>{{subOrderList.sum('Quantity')}}</span>
<span ng-bind="subOrderList.sum('Quantity')"></span>
If you actually need this value to be updated by user input then the HTML would look like this:
<input ng-model="Order.Quantity" type="number">
And then you will need to manually update that value in a controller or service when needed:
Order.Quantity = subOrderList.sum('Quantity');
From your comments it almost seems like you need an input that also changes dynamically and sporadically, but without a data example or more code I can't really see how that would work.
I'm aware that you can use $scope.formName.fieldName.$error.required to find out if a specific field has a required attribute which has not been fulfilled. You can also use $scope.formName.$error.required to find all fields in a form that have an unsatisfied required attribute.
I'm looking to find out weather or not a field is required. This is regardless of weather or not the user has already filled in that field. How can I do this?
N.B. as a result I want something like this {{countFilledRequiredFields()}}/{{countAllRequiredFields()}}, then I can tell the user something like: you have filled in 3/5 of the required fields
You can get all fields that have satisfied the required attribute from:
$scope.formName.$$success.required
And since you can get all fields that have an unsatisfied required attribute from:
$scope.formName.$error.required
You can easily combine the two to achieve what you want. i.e.
allRequiredFieldsCount =
$scope.formName.$$success.required.length +
$scope.formName.$error.required.length;
filledRequiredFieldsCount = $scope.formName.$$success.required.length;
So I tried to do something like this -
$('#price').val(price);
price is 300, and it shows good on browser, in input field, but when I want to take it out and mail it with PHP, in $_POST['price'] it doesn't show up, How can I insert something in inputs value with JavaScript, so I can mail it? It seems this is not an insertion in value, but just a feature to display something, correct?
Maybe this code can help you
document.getElementById('yorInputID').value = "Your Value";
There are a few possible reasons:
1) Your input field is not inside the form.
2) You are actually using a GET and not a POST.
Assuming that you can see the value updated in Firebug or Chrome's equivalent, it's gotta be one of those. Switch over to using $_REQUEST and see if that changes anything.
Your input for #price needs to also have a name "price"
<input id="price" value="price" />
From your question I'm assuming that this input is hidden -- and if that's the case I want to advise you not to rely on hidden fields + Javascript to provide you with security. It's so easily hackable I wouldn't even call it hacking.
Make sure the input is not "disabled" when the form submits.
if it's disabled the form don't send it.
so I am trying to implement the Jquery .serializeArray() method to transform a form into a JSON string to send it out in a request. This works great for my form except for checkboxes and radio buttons. The only time this works properly is when they are checked. Sometimes, I need to know if they are unchecked. They still need to get serialized.
I suppose I could manually loop through the form and grab the values and build the JSON object, but that would not be optimal.
According to the Jquery documentation found here: Jquery Docs anything that fits the W3 standards for a successful control found here should get included. Unfortunately, this does not include checkboxes that are not checked. Does anyone have a work around for this behavior? Thanks in advance...
var form = document.getElementById('f');
console.log($(form).serializeArray();
That spits out the serialized form with the checkboxes that are not checked excluded...
If you really want to use checkboxes and radio buttons, have those input fields update a corresponding hidden field. That way, the hidden input will always be sent.
how about trying this, I had a similar problem, I thought unchecked checkboxes should have a value as well, here is a quick work around,
add an extra class on each checkbox on your form "cbx"
make data an array from the form with serialise
then loop through all checkboxes with a class of "cbx"
and add them to the array with a value of 0, AFTER the array has been created with (serializeArray())
when you post the data you will see the unchecked checboxes and values of 0 will get transferred with the post.
var data = $('#form').serializeArray();
$(".cbx:not(:checked)").each(function() {
data.push({name: this.name, value: '0' });
});
$.post("testpage.asp", data);
An alternative would be to use a jQuery plugin. For example, serializeJSON has the option to set the value for unchecked checkboxes like this:
$('form').serializeJSON({checkboxUncheckedValue: "false"});
In any case, it is usually best to use hidden inputs for unchecked values.
Alright, so I developed a workaround.
Basically I wrote a function to compare the original JSON object to the serialized form. As I looped through, I compared the components. If there was a discrepancy, I pulled the component off the form and manually inserted it into the JSON. I used the array.splice() method to add the missing components. Worked for all of the missing inputs.