I'm just coming up to speed on jQuery, I'll apologize up front if this is a rookie error.
I'm attempting to get a simple custom trigger to fire without and success. Maybe someone can help help out and show me why this isn't working.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jQuery Trigger example</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$('#asset1').bind("triggerAssets", function() {
alert('Assets');
});
</script>
<script>
$(document).ready(function(){
$('#asset1').trigger("triggerAssets");
});
</script>
</head>
<body >
<div id="asset1" class="assets" >Asset
<table>
<tr>
<td>Field One: </td>
<td><input type="text" name="fieldOne" id="exfieldOne" value=""/></td>
</tr>
<tr>
<td>Field Two: </td>
<td><input type="text" name="fieldTwo" id="exfieldTwo" value=""/></td>
</tr>
<tr>
<td>Field Three: </td>
<td><input type="text" name="fieldThree" id="exfieldThree" value=""/></td>
</tr>
</table>
</div>
<hr>
</body>
</html>
Your binding function will run before the page is rendered; #asset1
doesn't yet exist. Move it into the ready
handler, which fires after the DOM is ready (and your element is present).
$(document).ready(function(){ // wait until the DOM is ready, then do:
$('#asset1').bind("triggerAssets", function() {
alert('Assets');
});
$('#asset1').trigger("triggerAssets");
});
you need to bind the event either when the DOM is ready or add a delegate on future elements with id of asset1:
$(document).ready(function(){
$('#asset1').bind("triggerAssets", function() {
alert('Assets');
});
$('#asset1').trigger("triggerAssets");
});
If you need to do something before the page is loaded, define an object to handle things like:
<script>
var test = {};
$(test).bind("test", function(){
alert("Your trigger was fired!");
});
$(test).trigger("test");
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.