admin 管理员组文章数量: 1086019
I am performing some crude operations in PHP without refreshing the page.
I don't understand this on()
function, what does it do? And what is the parameter #add
doing here?
$(document).on('click','#add',function(){
var name = $('#name').text(); //name & lname-table attribut names
var lname= $('#lname').text();
if(name =='')
{
alert("Enter name");
return false;
}
if (lname=='')
{
alert("Enter last name");
return false;
}
})
I am performing some crude operations in PHP without refreshing the page.
I don't understand this on()
function, what does it do? And what is the parameter #add
doing here?
$(document).on('click','#add',function(){
var name = $('#name').text(); //name & lname-table attribut names
var lname= $('#lname').text();
if(name =='')
{
alert("Enter name");
return false;
}
if (lname=='')
{
alert("Enter last name");
return false;
}
})
Share
Improve this question
edited Feb 20, 2017 at 10:31
dakab
5,92510 gold badges46 silver badges70 bronze badges
asked Feb 20, 2017 at 9:51
Celin MatthewsCelin Matthews
712 gold badges3 silver badges11 bronze badges
5
- That's JavaScript, specifically the jQuery library. – halfer Commented Feb 20, 2017 at 9:52
-
This isn't PHP; this is javascript; and
on
is used to set an event – Mark Baker Commented Feb 20, 2017 at 9:52 -
1
#add
is anid
reference to a particular element in your HTML.on()
is a way of attaching a behaviour to an event, in this caseclick
. – halfer Commented Feb 20, 2017 at 9:53 - basically its a event attacher – Dev Man Commented Feb 20, 2017 at 9:55
- check this w3schools./jquery/event_on.asp – Kamuran Sönecek Commented Feb 20, 2017 at 10:33
2 Answers
Reset to default 3The on() method attaches one or more event handlers for the selected elements and child elements.
Read more about jQuery on() Method
As per your code $(document).on('click','#add')
, I guess it's creating a delegated event.
It will directly fired on #add
id element
That's jQuery related. Check de 'on' documentation in jquery.
In your php page you must have an element with the ID 'add'. This jQuery code is executed when the users performs a mouse click on the 'add' element.
本文标签: jqueryWhat does on() in JavaScript doStack Overflow
版权声明:本文标题:jquery - What does on() in JavaScript do? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744024532a2520396.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论