在 JavaScript 中从一次点击事件到其他事件获取价值
Get value from one click event to other event in JavaScript
我有其他人写的代码。
<button onclick="javascript:confirm("Are you sure?");" id="confirm_button"/>
现在,为了向此按钮添加更多功能,我将一键式事件附加到同一按钮
$("#confirm_button").live("click",function(event){
//Need value from the confirm box.
//code goes here.
});
如何从事件侦听器中的第一个事件中获取值?我需要我的代码是否可以工作。一种方法是我必须将此确认框移动到我的单击事件侦听器中,但要求不允许我。谢谢
你可以声明一个全局变量和存储值离子,所以它可以在其他函数中使用
有点像这样
<script>
//global variable here
var testvariable;
function1
{
//initalize value here
}
function2
{
//use here
}
</script>
Global Scope variables
可能会帮助您解决问题。如果一个variable
是在function
之外声明的,那么这个variable
存在于global object
上。
查看此链接。您将获得有关各种scopes
的大量信息.. :)
<script>
var globalVariable= "someValue";//Use this variable anywhere u may need..
</script>
http://learn.jquery.com/javascript-101/scope/
在 jquery 中使用 confirm 像这样:
$("#confirm_button").live("click",function(event){
var valid = confirm("Are you sure?");
if(valid){
//do something
var myVal = $("YOUR SELECTOR").val(); // to get the value of your selector.
}else{
// do something else
}
});
试试这个...
$("#confirm_button").live("click",function(event){
var isvalid = confirm("Are you sure?");
if(isvalid){
// to get the value of your selector.
}
else
{
// do something else
}
});
<input type="button" onclick="var tmp=confirm('Are you sure?');if(tmp) foo(); "
id="confirm_button" value="btn"/>
//and in javascript function write ur code
function foo()
{
alert('add functions');
}
相关文章:
- 执行php函数的onclick事件的其他替代方案
- 单击其他元素时,我如何使用 jQuery 忽略更改事件
- 如何查明鼠标按下事件是否发生在滚动条上或元素中的其他任何位置
- 为什么不'我的窗口滚动事件根本没有启动.其他答案没有解决问题
- 从其他元素上的单击事件访问image src属性
- Phonegap iOS-设备准备不发射其他事件做
- onbeforeunload事件在我的代码中没有触发,但其他示例可以工作
- 如何将单击事件绑定到其他类
- 停止内部事件处理程序冒泡的其他方法,jQuery
- 存在其他参数时访问Javascript事件
- 如何在不干扰网站其他部分的情况下,在画布上捕捉keydown事件
- Ajax 请求正在取消页面上的其他 JS 事件
- 可以使 jquery 点击事件在所有其他点击事件之后执行
- 捕获在Windows应用程序中启动其他应用程序的用户事件
- 如何使用cURL或其他方式在点击时执行事件
- 当其他事件发生时,用javascript隐藏的文本框将变为可见
- ext.js中还有其他事件我应该使用吗
- 将事件绑定到其他UI组件's函数,在Kendo MVVM中
- 更改时事件会触发其他事件
- 如何根据其他组件中的事件更新组件URL