How to assign value to .click event - php

I have many input field that post data when enter key is press i have done one thing that i find id to of selected button and want to pas with click event bt it gives error here is code
$(".text").bind("keydown", function(event) {
// track enter key
var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
if (keycode == 13) {
var id=$(this).parent().find("input[type=submit]").attr('id');
(id).click(); // this one give error
return false;
} else {
return true;
}
});
(id).click(); this line gives error that id.click is not a function.what i m missing.

I think you are looking for:
$(this).closest("form").find("input[type=submit]").trigger("click");
There is no need to try to find the id and create a different JQuery object. You will have a reference to what you are looking for with find("input[type=submit]").
In order to know for sure, we would need to see your markup in order to find the most effective way to find what element you are looking for,

id is a string.
Strings don't have a click method.
Instead, you should submit the form directly:
$(this).closest('form').submit();

You need to use the following:
$("#"+id).trigger("click");

Related

prevent characters in text box in php using jquery

I want to prevent user to input few characters like ; and \ and /. I now it need jquery to do, but i cant find one
The jquery will used in name. zip code input to prevent such characters etc.
please show me an example as i new to jquery and php
You can use event.which to getting code of pressed key and use return false to preventing enter of character.
$("input").keydown(function(e) {
if (e.which === 186 || e.which === 191 || e.which === 220){
console.log("This character can't entered");
return false;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input />
This should do the job. I hope you can read this simple code:
// select css classes 'input' and assign onKeyDown event to them
$(".input").keydown(function(event) {
// check what key is pressed. dot is not allowed in this case
if ( event.keyCode === 190) {
// prevent event to happen
event.preventDefault();
}
});
On the bottom of this page there is a small table for some codes and also an input field which shows you key code for particular key. Just as a helper, to let you find codes for wished key faster.

Disable zend form element Enter keypress event

Does anyone know how to disable the Enter keypress event of a zend element text input control? When a user presses Enter on a form element it tries to post, which I want to disable.
This is something you'll need to do client side (i.e. with Javascript). With Dojo it would be something like:
dojo.connect(dojo.byId('FIELDID'), 'onkeydown', function(event){
if (event.keyCode == dojo.keys.ENTER) {
dojo.stopEvent(event);
}
});
replace 'FIELDID' with the ID of the text field you want to hook this into. You could combine this with a dojo.query call if you want to apply it to everything in the form.
jQuery and the other JS frameworks will have an equivalent.
I think this Should solve your problem
$textField = new Zend_Form_Element_Text("text");
$textField->setAttrib('onkeypress', 'nullifyEnterKey();');
function nullifyEnterKey(e)
{
var key;
if(window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
return (key != 13);
}

show div when option already selected

Hi guys i using this jquery to show a div when i select a option
$("#select1").change(function(){
if ($(this).val() == "2" ) {
$("#hide1").fadeIn("fast");
} else {
$("#hide1").fadeOut("fast");
}
});
});
But i have a problem when i load the page get results from database, the option on select return SELECTED and the div only shows if i change the option..
can samebody help what i neeed to do to solve this?
explain again: get the val() from select and show the div without change the option... sorry for my english..
thanks
You can trigger the change after you bind it.
$('#select1').change( function(){
// your change code
if ($(this).val() == "2" ) {
$("#hide1").fadeIn("fast");
} else {
$("#hide1").fadeOut("fast");
}
}).change();
When the page loads (and after you bind the onchange function to your form elements) you should trigger any of these functions:
$(document).ready(function(){
$("#select1").trigger('change');
});
This way it will act as if the user selected it even if the value was loaded from the database.
EDIT: czarchaic's answer is also very good, as chaining the event trigger to the event binding is more compact and it may be easier to read the code, and in this instance is probably the better solution. The answer I provided will work fine as well, and while it's longer, it as the small advantage that it will also work in cases where the function was not bound by jquery (i.e. when the event is explicitly declared in the HTML):
<select id=""select1" onchange="if ($(this).val() == '2' ) {
$('#hide1').fadeIn('fast');
} else {
$('#hide1').fadeOut('fast');
}
">
<option...
You can do something like this. Triggering the change event after binding it.
$("#select1").change(function(){
if ($(this).val() == "2" ) {
$("#hide1").fadeIn("fast");
} else {
$("#hide1").fadeOut("fast");
}
}).change();

Check in ajax/jquery a form beform submit ( check the value whether it is in database)

ajax is not yet sothin i master.
I have two forms field
code :
name :
and the submit button like :
<form><input type=text name=code><input type =text name=name/></form>
I would like in php/jquery to check if the code the user fill exist in a table of my db.
If it does not exits, when the user leave the textfield to fill the next one, i would like to print a message like: this code is not in the db and then clean the fied. Until the user provide a valide code.
If your php service returns true or false for validation.
and the placeholder for the error is a label called
then an example (in jQuery) would be
$(document).ready(function() {
$("form").submit(function(e) {
var code = $("input[name='code']");
var error = $("#error");
e.preventDefault();
var form = this;
$.getJSON('urlToPhp',
{ code: code.val() },
function(valid) {
if (!valid) {
error.text(code.val() + ' is not found try another code...');
code.val('');
} else {
form.submit();
}
}
);
});
});
I've created a simple example at http://jsfiddle.net/nickywaites/e4rhf/ that will show you have to create a jQuery ajax post request.
I'm not too familiar with php so that part of it I'll have to leave aside although you can use something along the lines of $_POST["Name"].
Here is php example that I googled http://php4every1.com/tutorials/jquery-ajax-tutorial/ that might be better for you.

jquery, checkboxes within nested div onclick show/hide submit

I have a page that has a series of div's in a shopping-cart system. Each is a product, when checking the checkbox, if there are options available and/or an acknowledge checkbox to be acknowledged before form submit, then keep the button hidden and show a div with another checkbox in it, when that checkbox if exists is checked then show button.
The show hide on each div is working properly (existing code), So I am trying to create a function to be called within that function for the onclick event.
where my issue arises is the check to see if another check box [shown in the example as(tmpId == "acknowledge")] does exist and that it is checked also before they can see the submit button, if not show the submit button. This would need to work for each product they click the first checkbox on [ie("id= "+feeSID+"_checkbox")]... $("#ocContainer") is the hidden container(with submit) to show if proper checkboxes are checked.. for each product selected.
the first part of each id is a variable generated by a sql query and passed to the function..
My Function:
var feeSID = "";
var feeVAL = "";
var pop = "";
function countChecked(feeSID) {
var n = $("input:checked").val();
var tmpId = $(this).attr("name");
if (tmpId == "acknowledge") {
console.log("acknowledge Clicked <br />");
} else {
feeVAL = $(this).attr("id");
}
if (feeVAL == '') {
return;
} else {
if ($("#" + feeSID + "_checkbox:checked")) {
console.log("id= " + feeSID + "_checkbox");
var pop = $("#" + feeSID + "_popup").length;
if (pop > 0) {
$(":checkbox").click(function () {
if (tmpId == "acknowledge") {
$("#ocContainer").show();
}
});
} else {
$("#ocContainer").show();
}
}
}
}
I hope my explanation was clear. I am getting an error of:
uncaught exception: Syntax error, unrecognized expression: #[object Object]_popup
I think that the exception is possibly caused by this line in your code:
var pop = $("#"+feeSID+"_popup").length;
At some point in your code, feeSID is assigned to an object. So, you are trying to use an object as part of your selector rather than a string. You need to make sure that feeSID is a string rather than an object before you try using it as part of the selector.
The error was caused by me trying to use an Obj as part of my selector. As Adam Prax had pointed out
var pop = $("#"+feeSID+"_popup").length;
At some point in my code, feeSID is assigned to an object. I solved it by redeclaring the Variable or Object as a string before accessing it again using new String(feeSID);
This solved the issue. I hope it helps someone else, all I could find on google pointed to the use of the syntax #name="name" which is no longer valid syntax in jQuery.

Categories