First of all thanks in advance. I am new to javascript and I am facing an issue with a form that uses javascript to submit values. Everything works great, but the values are submitted as a string delimited by comma, and there is a possibility that the values are submitted have comma on it, so when I explode it on php, I mess up the form input. By example:
Form input 1: Highschool, Year 2
Form input 2: City
So, the resulting string is: Highschool, Year 2, City
and I need it to be: Highschool, Year 2||City
I can explode it on php and use the values, but no clue how to. I would really appreciate any advice.
Here is the javascript code:
function vpb_submit_items()
{
var vpb_items = [];
$.each($('.vpb_item'), function()
{
vpb_items.push($(this).val());
});
if(vpb_items.length == 0 || vpb_items.length == "")
{
vpb_items = "Empty Field";
}
var dataString = "items="+ vpb_items;
$.ajax({
type: "POST",
url: "process.php",
data: ""+dataString+"&date="+date,
Thanks again.
Use Array's join() method:
var dataString = "items="+ vpb_items.join('||');
Related
I am trying to use some Javascript in a PDF form. As an example, let's say I have two fields: let's call them TextField1 and TextField2.
These fields can be used independently, but when TextField1 has a certain value, I'd like TextField2 prefilled.
This works fine (triggered onBlur, not that it matters):
var f = this.getField("TextField1");
var g = this.getField("TextField2");
if (f.value == "123") {g.value = "foobar";}
My problem is this: How can I compare against a substring of f? I'd like the action to trigger when the first char of f equals "1", but
if (f.value.substr(0,1) == "1") { … }
or similar have not worked.
Try to make a string instance from value.
You can use one of the following methods:
var fStr = f.value + '';
var fStr = String(f.value);
and then try to test fStr in a manner you mentioned above: fStr.substr(0,1) === 'a'
I'm using a variation of this script to combine two numeric fields into a third numeric field but I'm wanting to do the same thing with text. Namely creating a Full_Name field from a First_Name and a Last_Name field. Here's the code...
Hoping someone can help me! Or maybe "keyup" function just doesn't work for non-number fields?
var ctrlFirst_Name = Runner.getControl(pageid, 'First_Name');
var ctrlLast_Name = Runner.getControl(pageid, 'Last_Name');
var ctrlName = Runner.getControl(pageid, 'Name');
function func() {
ctrlName.setValue(ctrlFirst_Name.getValue() + (ctrlLast_Nme.getValue()));
};
ctrlFirst_Name.on('keyup', func);
ctrlLast_Name.on('keyup', func);
Thanks!
Eric
I am currently trying to take all changes made to a form and put it into a JSON. If there are no changes than the JSON is empty. The form contains values that are strings, ints, and floats. So, I cannot cast them all as a specific type.
This wasn't an issue until I ran into the result form the console.log statement batchsize:string 1.0 does not equal string 1. Obviously this is correct in saying the two strings are not equal, but I am having trouble with finding a way that allows me to compare them without this being an issue. Does anyone have any advice
function getChanges()
{
//Get All User made changes form the website
var returnJSON = "{ ";
$('#form *').filter(' input:not([type="submit"])').each(function(){
var current = this.value;
var original = this.getAttribute('value')
var id = $(this).attr('id');
if((id!=="prod")&&(id!=="prodamt")&&(id!=="subtotal")&&(id!=="matlamt")&&(id!=="tax")&&(id!=="total")&&(id!=="matl")&&(id!=="prod-detail-formula-price")&&(id!=="prod-detail-formula-taxable")) //this ones for you zoe
if(current !== original)
{
returnJSON += '"'+id+'" : { "original":"'+original+'", "modified":"'+current+'"},';
console.log(id+":"+typeof original+ original +" does not equal " +typeof current+current);
}
});
returnJSON = returnJSON.substr(0, returnJSON.length-1);
returnJSON += '}';
return returnJSON;
}
use $.isNumeric() and if both are numeric check are they equal as a numbers using parseFloat or parseInt to convert to numeric
My Javascript var contains a 2D array.
If I pop an alert on the the var i get the JSON serialized result, something like:
ID0, DESCRIPTION
I'd like to get each items separated by the , in the value option of the dropdownlist and the other item in the description.
Here's my Javascript code, it would work if split was working correctly but this pops an error because the var doesn't contain a pure string type.
$.ajax(
{
type: "POST",
url: "Projet.aspx/GetDir",
data: "{VP:'" + dd_effort_vp + "',DP:'" + dd_effort_dp + "',Direction:'" + dd_effort_d + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
var cars = response.d;
$.each(cars, function(index, value) {
$('#<%= dd_effort_directionp.clientid()%>').append(
$('<option</option>').val(value[value.split(",",0)]).html(value.split(",",1))
}
}
});
I know split doesn't work that way here because of the return value is not a string but you get the result i'd like to achieve, get the first value before the comma has the VALUE of the Dropdownlist and the item after the comma as the HTML text.
Thanks ALOT!
How about value.split(",")[0] instead of value.split(",",0)?
Have you tried value.toString().split(",")?
I am using an autocomplete Javascript function, and I need to extract the last 5 characters from 'value' and then compose URL for onSelect.
The function I am using is:
<script type="text/javascript">
var options, a;
jQuery(function(){
var onAutocompleteSelect = function(value, data) {
window.open('ITEM.PRO?id='+ value);
}
options = {
serviceUrl:'JQUERY-SEARCH.pro',
onSelect: onAutocompleteSelect,
};
a = $('#query').autocomplete(options);
});
</script>
When I click on search result it loads "ITEM.PRO?id=Article Brand Model Year Description 35612", but I need load "ITEM.PRO?id=35612"
Please could you help me? I am a totally newbie with JS.
Thank you all in advance!
Instead of window.open('ITEM.PRO?id='+ value); could you do this?
window.open('ITEM.PRO?id='+ value.split(' ').pop());
There are a few different ways to acheive this.
This simplest is to add
value = value.slice(-5);
right before
window.open('ITEM.PRO?id='+ value);
This sets value to its last 5 characters. Read here about the String.slice function.
If you want set the value to the last 'word', so to speak, delimited by spaces, you could do this instead:
value = value.split(" ").pop();
Another method would be to take the last continuous string of digits in the value. For that, you could use this:
value = value.match(/\d+/).pop();
Which method you use, of course, depends on what would work most reliably with the input you have.
Try this
var onAutocompleteSelect = function(value, data) {
window.open('ITEM.PRO?id='+ value.substring(value.length-5));
}
This is a terrible solution, but will work in the case you listed. I will edit if you post more details:
<script type="text/javascript">
var options, a;
jQuery(function(){
var onAutocompleteSelect = function(value, data) {
window.open('ITEM.PRO?id='+ value.match(/\d+/)[0]);
}
options = {
serviceUrl:'JQUERY-SEARCH.pro',
onSelect: onAutocompleteSelect,
};
a = $('#query').autocomplete(options);
});
</script>
value.match(/\d+/)[0] will match any digits in your string as any array, so we take the first item in that array.
When your IDs exceed 5 digits, your code will break (as has been stated in comments). You can also use the .split approach mentioned by #bordoz, the disadvantage being that spaces in any of the other words would break this solution. Or you could use:
var url = 'ITEM.PRO?id='+ value.replace(/[^\d\.]/g,'');
Which would fail only if any of the other word contain numbers. Which one best fits your situation?