JS / Jquery中的全局变量在函数之间(Global Vars in JS/Jquery between functions)

编程入门 行业动态 更新时间:2024-10-25 04:25:39
JS / Jquery中的全局变量在函数之间(Global Vars in JS/Jquery between functions)

当试图从另一个函数中全局定义的变量中获取值时,我一直未定义,但是我理解全局变量在函数之间工作。

我希望能够在主要单击函数中获取var值,并在函数nu()中使用它们,而无需重用

var itemTitle = $(“input#add_item_item_title”)。val();

为了定义另一个时间。

我确信这是可能的,但也许我错过了一些东西。

JS / JQ

$(document).ready(function(){ init(); }); function init() { $(function() { $('.error').hide(); $(".add_item_btn").click(function() { // validate and process form here $('.error').hide(); var itemTitle = $("input#add_item_item_title").val(); if (itemTitle == "") { $("label#add_item_item_title_error").show(); $("input#add_item_item_title").focus(); return false; } //add_item(); nu(); return false; }); }); } function nu(){ // when using this the var is defined but would rather not have to reuse this// var itemTitle = $("input#add_item_item_title").val(); ///////////////////////////////////////////////////////////////////// var url = "script.php"; $.ajax({ type: 'POST', url: url, dataType: 'html', data: { itemTitle: itemTitle, groupID: groupID, unitPrice: unitPrice }, beforeSend: function() { document.getElementById("message").innerHTML="<span class='blink_me'>Sending..</span>" ; }, success: function(html) { document.getElementById("message").innerHTML=itemTitle; } }); }

When attempting to grab values from vars globally defined within another function, i keep getting undefined, yet it was my understanding global vars work between functions.

I would like to be able to get the var values within the main click function, and use them within function nu() without having to reuse the

var itemTitle = $("input#add_item_item_title").val();

in order to define another time.

I was sure this is possible, but perhaps im missing something.

The JS/JQ

$(document).ready(function(){ init(); }); function init() { $(function() { $('.error').hide(); $(".add_item_btn").click(function() { // validate and process form here $('.error').hide(); var itemTitle = $("input#add_item_item_title").val(); if (itemTitle == "") { $("label#add_item_item_title_error").show(); $("input#add_item_item_title").focus(); return false; } //add_item(); nu(); return false; }); }); } function nu(){ // when using this the var is defined but would rather not have to reuse this// var itemTitle = $("input#add_item_item_title").val(); ///////////////////////////////////////////////////////////////////// var url = "script.php"; $.ajax({ type: 'POST', url: url, dataType: 'html', data: { itemTitle: itemTitle, groupID: groupID, unitPrice: unitPrice }, beforeSend: function() { document.getElementById("message").innerHTML="<span class='blink_me'>Sending..</span>" ; }, success: function(html) { document.getElementById("message").innerHTML=itemTitle; } }); }

最满意答案

删除变量声明中的var 。

澄清:您使用var定义该变量,因此只能在该函数的范围内访问它,并且只能在其中定义任何函数。 如果你只使用myVar = 15; (没有var ),变量将在全局范围内定义。 在浏览器中,这相当于使用window.myVar = 15 。

进一步澄清:使用全局变量通常意味着你正在做一些你不应该做的事情。 我不是一个纯粹主义者,但我觉得很多人都会哭泣。 在您的回调函数被触发之前,您正冒着将该全局变量重新分配给另一个值的风险 - 调试此类范围问题可能会很麻烦。

在您的示例中,为什么不将变量传递给函数,如nu(itemTitle); :

function nu(itemTitle){ /* blah blah blah */ /* * itemTitle is now available here and directly * in any functions that are defined in this * part of the code */ }

Remove the var in your variable declaration.

Clarification: You're defining that variable with var so it is only accessible within the scope of that function and any functions defined immediately within it. If you just use myVar = 15; (without the var), the variable will be defined within the global scope. In the browser, this is equivalent to using window.myVar = 15.

Further clarification: Use of global variables usually means you're doing something you shouldn't be. I'm not a purist, but I get the feeling that many people will cry foul. You're running the risk that you reassign that global variable to another value before your callback function has been triggered - it can be a pain in the butt to debug such scoping issues.

In your example, why not just pass the variable to the function, like nu(itemTitle);:

function nu(itemTitle){ /* blah blah blah */ /* * itemTitle is now available here and directly * in any functions that are defined in this * part of the code */ }

更多推荐

本文发布于:2023-08-07 18:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465120.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   全局变量   Jquery   JS   Vars

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!