I'm trying to disable a form in a JSP just when two conditions become true. I've tried a var variable set false so when my if-else condition becomes true it becomes true. But it is not working.
Here is my code. (not sure if it can help but I'm using eclipse)
var disabled=false;
then I've created the function
function isDisable(){
if($("#idCost").val() == 1 && $("#IdBasket").val() == "3"){
disabled=true;
}
}
and then set in the form field
< disabled="false"/>
What am I doing wrong?
Thanks for helping
It looks like your code is in JavaScript and not Java, even though you say you are writing a JSP.
Your JS code compare one return to a Number (val() == 1) and another to a String val() == "3". Did you try making them both the same? If .val() is returning strings, then they should both compare to strings. If .val() is returning numbers, then they should both compare to numbers.
Related
I am developing a webpage with fairly simple JavaScript. My entire JavaScript code is:
function showUnlockPopup(isViolated,instId,unlock_done,unlock_total,period,endDate){
alert(isViolated);
if(isViolated){
if(unlock_done < unlock_total){
showInfoPopup(instId,unlock_done,unlock_total,period);
} else {
showNoUnlocksPopup(instId,unlock_done,unlock_total,period,endDate);
}
} else {
showNotViolatedModal();
}
}
However, irrespective of the value of the 'isViolated' variable, my 'showInfoPopup' function is called.
I have checked the code for the method call too:
<button onClick="showUnlockPopup(isViolated)">Unlock</button>
The alert shows 'true' or 'false' values correctly, but the logic runs for only 'true' irrespective of the value.
I think I am missing something fairly basic here. I tried a lot, but of no use. Kindly help me with the same.
it is because isViolated is returned as a string. so unless the string is null it will be true if it has some contents. You should change your check to isViolated == 'true', make a new variable that is a boolean and assign it depending on isViolated or something third.
I'm very new to JS - only a couple days in.
Trying to write a very basic prompt evaluated by an if statement.
When I run the code below, the user is prompted, but the statement is never evaluated by the if statement.
Any help? -- I realize the answer is probably simple and obvious, but as a SUPER beginner, what do I do?
var bool = prompt("What is an example of a boolean?");
if (typeof(bool) === "boolean") {
print("correct! that is a boolean");
print(bool) ;
};
In this case, assuming the user inputs something in the prompt, the type of the bool variable will always be a string. You'd rather check if the input compares to the string "true" or "false" etc., like this:
if (bool.toLowerCase() == "true" || bool.toLowerCase() == "false") {
...
}
I moved a site from one location to another. However i noticed when i moved it some items in my java script stop working. I have been trying to figure out where this went wrong but this is my last resort. Here is my fiddle site:
http://jsfiddle.net/robertpurpose/FCZVJ/6/
if ($("#Deg0").attr('checked') && $("#Res0").attr('checked')) {
$("#TuitionPerHour").val(202.83);
}
if ($("#Deg0").attr('checked') && $("#Res1").attr('checked')) {
$("#TuitionPerHour").val(202.83);
}
if ($("#Deg0").attr('checked') && $("#Res11").attr('checked')) {
$("#TuitionPerHour").val(202.83);
}
if ($("#Deg0").attr('checked') && $("#Res2").attr('checked')) {
$("#TuitionPerHour").val(parseFloat(243.39).toFixed(2));
}
if ($("#Deg0").attr('checked') && $("#Res3").attr('checked')) {
$("#TuitionPerHour").val(parseFloat(304.24).toFixed(2));
}
Feel free to be as blunt as you want. We all live and learn. I take everything as a learning experience
The problem is that you are using $.attr('checked'). That used to return a boolean, but it now returns the attribute value (string) 'checked'. HTML - Why boolean attributes do not have boolean value?
So your check
if ($("#Deg1").prop('checked')) {
$("#TuitionPerHour").val(294.44);
}
Is always passing. Change all your $.attr to $.prop to http://jsfiddle.net/FCZVJ/7/
Other Problems with your code
Your jsfiddle JS needs to be in the head so your function updateTotal is global
Store your jQuery variables (you're calling $("#Deg0") a million times)
eval(document.CostEst.FoodPlan[TheIndex_FoodPlan].value)); Crapola, using eval to convert a string to a number? Use parseInt() or parseFloat
$("#SemesterHours").val() <= 7 does work because of auto coercion, but note that if it said $("#SemesterHours").val() <= $('#somethinElse').val() you'd be comparing strings and "17" > "9" === false.
parseFloat(243.39).toFixed(2) is an eye sore (code smell). You're asking to parseFloat on something that is already a float. You probably did that because you wanted to call toFixed on it and you can't just use 243.39.toFixed(). You should use either Number(243.39).toFixed() or (243.39).toFixed()
I am using jQuery to create a client-side validation function for a .NET form. Each form element has an id and several of the form elements are required fields.
In my validation script, I thought of creating an array of the id's of the 'not required' elements, then on every 'blur' event checking whether or not the current element ($(this)) is part of the array of elements not to check, but it doesn't seem to be checking against the list.
function validate(){
$('.form_wrapper input').blur(function(){
var isEmpty = $(this).val();
var isRequired = $(this).attr('id');
var notRequired = ['txtHomePhone','txtWorkPhone','txtMobile','txtStreetAddress','txtSuburb'];
if (isEmpty == "" && isRequired == notRequired){
// run conditional validation stuff
}
else {
// run other conditional validation stuff
}
});
}
The area I think I need help with is the if statement checking whether or not the current form element is part of the array of id's not to validate. I am also not really sure if it's actually an array I want/need to use in this situation?
Any help would be great,
Thanks,
Tim
not exactly sure here, but wouldn't you want to be doing
$.inArray(isRequired,notRequired) >= 0
instead of
isRequired == notRequired
EDIT
$.inArray() returns -1 if no match is found. Modified code to correctly show this behavior.
Currently I have a race condition existing in my JavaScript code. What I am trying to do with this code is convert all check boxes which are a part of the 'checkbox' class and are not checked into text boxes with a value of zero. Currently when you post a check box that is not checked it does not appear in the $_POST data. However I need to know all the values whether true or false for these particular set of check boxes.
The code I have is here:
Code:
function checkboxConvert() {
var chkBxs = $$('.checkbox');
for (var i = 0; i < chkBxs.length; i++) {
if (chkBxs[i].checked == false) {
chkBxs[i].type = 'textbox';
chkBxs[i].value = '0';
}
}
setTimeout("document.productForm.submit();",1000);
}
Now the problem that I have been getting is that when I try to submit this form the values of the recently changed text boxes does not appear in the $_POST data. Therefore, as you can see above I have postponed the page submit for 1 sec and then I have all the data available to me. However as time goes on and my data set gets larger, 1 sec may no longer be enough. This I think is a race condition and I need to figure some way of running the code only after all the check boxes have been converted and they have their new values. I would have thought that this would be unnecessary from the start, but for some reason it's trying to run both pieces simultaneously and I can't submit until I have the proper values in place.
Any help is much appreciated!
This is definitely not the way to do web. I strongly advise you abandon your checkboxConvert function, and solve this issue on the server side
JavaScript always runs single-threaded in the browser so I don't think it can be a race condition.
I'd generally agree with others that you shouldn't do this, but your problem may be that you're changing the element to a type of "textbox" instead of "text". If you declare an input of type "textbox" in HTML markup, it will usually render as a text field anyway because that's the default. However, changing an already valid "checkbox" type input to the invalid "textbox" may not work predictably.
Try changing it to this:
function checkboxConvert() {
var chkBxs = $$('.checkbox');
for (var i = 0; i < chkBxs.length; i++) {
if (chkBxs[i].checked == false) {
chkBxs[i].type = 'text';
chkBxs[i].value = '0';
}
}
// Because JS in the browser is single-threaded, this
// cannot execute before the preceding loop completes anyway.
document.productForm.submit();
}
There's got to be a better way to do this. Try something like:
Know about all your possible values on the server side. It looks like you're using PHP; keep a simple array with the names of your checkboxes.
When you take your $_POST data, remove the names of checkboxes you've received values for from your array.
The remaining are all false.