[英]button onclick function not work properly in jquery
i am trying to execute external function using button onclick event in asp.net mvc application.But some resign it not work properly, i cant understand what the resion. 我正在尝试使用asp.net mvc application.on中的按钮onclick事件执行外部功能。但是有些人辞职不能正常工作,我无法理解该版本。
<input type="button" id="123" onclick="alert('123')" value="Button 1" />
<input type="button" id="124" value="Button 2" />
<input type="button" id="125" onclick="Show_id();" value="Button 3" />
<input type="button" id="126" value="Button 4" />
JQuery code here. jQuery代码在这里。
<script type="text/javascript">
$(document).ready(function () {
$("124").click(function () {
alert(this.id);
})
function Show_id() {
alert(this.id);
}
$("126").click(function () {
alert("message");
})
})
when Button 1 Click Then It work Properly.But remaining 3 bytton not work. 当按钮1单击``然后它可以正常工作'',但其余3个按钮不起作用。
$(document).ready(function () { $("#124").click(function () { alert(124); }) function Show_id(e) { alert(e.type); } $("#126").click(function () { alert("message"); }) })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="button" id="123" onclick="alert('123')" value="Button 1" /> <input type="button" id="124" value="Button 2" /> <input type="button" id="125" onclick="return Show_id(event);" value="Button 3" /> <input type="button" id="126" value="Button 4" />
Ive posted a jsfiddle here. Ive在这里发布了jsfiddle。 https://jsfiddle.net/1tsx7e6z/
https://jsfiddle.net/1tsx7e6z/
<input type="button" id="123" onclick="alert('123')" value="Button 1" />
<input type="button" id="124" value="Button 2" />
<input type="button" id="125" onclick="Show_id()" value="Button 3" />
<input type="button" id="126" value="Button 4" />
$(document).ready(function () {
$("#124").click(function () {
console.log('124');
});
$("#126").click(function () {
alert("message");
});
});
//Note its outside of ready() above. Also I use window.xyz here because of jsfiddle loading into 'load' scope by default, so im attaching to window. This is generally bad practice either way, avoid inline click function calls.
window.Show_id = function() {
alert(event.target.id);
}
You are missing # 您不见了#
Your show_id is in a different scope (its in document.ready) 您的show_id在不同的范围内(在document.ready中)
Don't call inline onclick events :) Its bad practice. 不要调用内联onclick事件:)这是一种不好的做法。 Use jQuery as you have for #126 to hook into one.
像#126那样使用jQuery,以使其合而为一。
Also don't ever allow user input to be added to oncliick="somethingFromTheUser" as they can easily inject javascript in places you may not expect (there's too many attack vectors to worry about adding it, just don't do it) 也永远不要允许将用户输入添加到oncliick =“ somethingFromTheUser”中,因为他们可以轻松地将JavaScript注入您可能不希望的地方(攻击向量太多,担心添加它,只是不要这样做)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.