JQuery 值未保存
JQuery value not saving?
单击它时我有两个div,它将ID值保存到变量中,该值正在保存到变量中,但在运行其他函数时未定义。
请看一下它应该更有意义.
链接
//Setting the click amount
var ClickedAmount = 1
//On a note click run...
$(".note").click(function() {
//If Click amount == 2 run
if (ClickedAmount == 2) {
//Alert NoteOne - This should be a value
alert(NoteOne);
};
//If Click amount is == 1 run
if (ClickedAmount == 1) {
//Get the ID of the element that was clicked on and
//replace note id with nothing.
var NoteClicked = this.id.replace('note', '');
//NoteOne - Now == the Divs number id Selected.
var NoteOne = NoteClicked
alert(NoteOne);
//Clicked amount added so other if statements runs on next click
ClickedAmount++;
};
})
有什么建议吗?
在这里你可以找到一个工作的小提琴。
NoteOne 变量是函数中的局部变量。函数执行结束后,变量将被遗忘。如果要保留该值,请将变量设为全局变量。
var NoteOne = null;
//Setting the click amount
var ClickedAmount = 1
//On a note click run...
$(".note").click(function() {
//If Click amount == 2 run
if (ClickedAmount == 2) {
//Alert NoteOne - This should be a value
alert(NoteOne);
};
//If Click amount is == 1 run
if (ClickedAmount == 1) {
//Get the ID of the element that was clicked on and
//replace note id with nothing.
var NoteClicked = this.id.replace('note', '');
//NoteOne - Now == the Divs number id Selected.
NoteOne = NoteClicked
alert(NoteOne);
//Clicked amount added so other if statements runs on next click
ClickedAmount++;
};
})
变量NoteOne
将被提升到顶部。因此,它显示未定义。如果要使其按照预期工作,请将该NoteOne
变量声明移到事件侦听器之外。换句话说,将其移动到该事件侦听器的词法范围。
var NoteOne;
var ClickedAmount = 1
$(".note").click(function() {
.
.
演示
你应该在函数之外声明NoteOne
:
//Setting the click amount
var ClickedAmount = 1
var NoteOne;
//On a note click run...
$(".note").click(function() {
//If Click amount == 2 run
if (ClickedAmount == 2) {
//Alert NoteOne - This should be a value
alert(NoteOne);
};
//If Click amount is == 1 run
if (ClickedAmount == 1) {
//Get the ID of the element that was clicked on and
//replace note id with nothing.
var NoteClicked = this.id.replace('note', '');
//NoteOne - Now == the Divs number id Selected.
NoteOne = NoteClicked
alert(NoteOne);
//Clicked amount added so other if statements runs on next click
ClickedAmount++;
};
})
.note {
width: 200px;
height: 50px;
margin-left: 5px;
margin-top: 50px;
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post">
<div id="note1" class="note">Note 1</div>
<div id="note2" class="note">Note 2</div>
<!-- The input section, user clicks this to login on. -->
<input id="submit" name="submit" type="submit" value="Login">
</form>
相关文章:
- Javascript/jQuery压缩一个图像正确的评论帖子,并使用Ajax Laravel 5.2保存到控制器中
- 将JQuery Handontable保存到服务器上的excel文件中
- JQuery Deferred Ajax,将结果保存在调用对象中
- 如何在jquery、javascript、HTML5中以base64字符串保存为(PDF、doc、xls、png.)
- 使用jquery交换并保存背景颜色
- jquery ajax到php脚本不保存数据
- 使用jQuery保存模式弹出数据
- 使用javascript或jquery保存输入字段值
- jQuery日期[时间]选择器-如何保存时间范围(+修改日期格式)
- 使用javascript/jquery从现有数组中创建一个新数组,该数组保存项目存在的次数
- jquery保存数据,直到phonegap应用程序关闭
- 即使在从DOM中删除后,也可以保存jQuery对象
- 保存 JQuery 表单中的输入数据
- 正在保存jquery中的数据
- 如何保存jquery动态生成的数据
- 更流畅的页面加载保存JQuery toggleClass设置
- 保存JQuery UI可拉件的顺序
- 如何在数组中保存jQuery对象以供以后使用?
- 保存JQuery UI可拖动位置与cookie,工作,但有轻微的偏离设置
- 永久保存jquery修改过的页面