I am trying to make a function which will execute when the value of a text input field
is changed. The value of the field changes when a table cell is clicked, not on keyup
, nor paste. I tried it like this:
$("#kat").change(function(){
alert("Hello");
});
And I have the text field:
<input type="text" id="kat" value="0"/>
but it isn't working. Any help?
This is from a comment on the jQuery documentation page:
In older, pre-HTML5 browsers, "keyup" is definitely what you're looking for.
In HTML5 there is a new event, "input", which behaves exactly like you seem to think "change" should have behaved - in that it fires as soon as a key is pressed to enter information into a form.
$('element').bind('input',function);
Seems to me like you are updating the value of the text field in javascript. onchange
event will be triggered only when you key-in data and tab out of the text field.
One workaround is to trigger the textbox change event when modifying the textbox value from the script. See below,
$("#kat").change(function(){
alert("Hello");
});
$('<tab_cell>').click (function () {
$('#kat')
.val($(this).text()) //updating the value of the textbox
.change(); //trigger change event.
});
This technique is working for me:
$('#myInputFieldId').bind('input',function(){
alert("Hello");
});
Note that according to this JQuery doc, "on" is recommended rather than bind in newer versions.
This worked for me
var change_temp = "";
$('#url_key').bind('keydown keyup',function(e){
if(e.type == "keydown"){
change_temp = $(this).val();
return;
}
if($(this).val() != change_temp){
// add the code to on change here
}
});
The jQuery documentation says this "It is also displayed [the handler execution result] if you change the text in the field and then click away. If the field loses focus without the contents having changed, though, the event is not triggered.".
Try the keyup and keydown events, like this:
$("#kat").keydown(function(){
alert("Hello");
});
function search() {
var query_value = $('input#search').val();
$('b#search-string').html(query_value);
if(query_value !== ''){
$.ajax({
type: "POST",
url: "search.php",
data: { query: query_value },
cache: false,
success: function(html){
//alert(html);
$("ul#results").html(html);
}
});
}return false;
}
$("input#search").live("keyup", function(e) {
clearTimeout($.data(this, 'timer'));
var search_string = $(this).val();
if (search_string == '') {
$("ul#results").fadeOut();
$('h4#results-text').fadeOut();
}else{
$("ul#results").fadeIn();
$('h4#results-text').fadeIn();
$(this).data('timer', setTimeout(search, 100));
};
});
and the html
<input id="search" style="height:36px; font-size:13px;" type="text" class="form-control" placeholder="Enter Mobile Number or Email" value="<?php echo stripcslashes($_REQUEST["string"]); ?>" name="string" />
<h4 id="results-text">Showing results for: <b id="search-string">Array</b></h4>
<ul id="results"></ul>
I recently was wondering why my code doesn't work, then I realized, I need to setup the event handlers as soon as the document is loaded, otherwise when browser loads the code line by line, it loads the JavaScript, but it does not yet have the element to assign the event handler to it. with your example, it should be like this:
$(document).ready(function(){
$("#kat").change(function(){
alert("Hello");
});
});
That is working for me. Could be a browser issue as mentioned, or maybe jQuery isn't registered properly, or perhaps the real issue is more complicated (that you made a simpler version to ask this). PS - did have to click out of the text box to make it fire.
This works for me on all browsers and Jquery <= v1.10
$('#kat').on('keyup', function () {
alert("Hello");
});
or as it seems you want
$('#kat').on('click', function () {
alert("Hello");
});
Textbox input field change event fires as you would expect it to, the jQuery .Change event only works correctly on html5 supported browsers
Try this,
$('#kat').on('input',function(e){
alert('Hello')
});
来源:https://stackoverflow.com/questions/10438369/jquery-on-change-input-text