简体   繁体   English

jquery变量从一个函数到另一个函数?

[英]jquery variable from one function to another function?

I have 2 functions where I'm looking to pass a variable called "url" to the second function. 我有2个函数,我希望将一个名为“url”的变量传递给第二个函数。

The first function gets an href attribute from the value of "url" as it spits out of my database and is a trigger to open a modal window. 第一个函数从“url”的值中获取一个href属性,因为它从我的数据库中吐出,并且是一个打开模态窗口的触发器。

 <a class=\"trigger3\" url=\"".$url."\" href=\"#\">

The function looks like this. 该功能看起来像这样。

 <script type="text/javascript">
 $(document).ready(function(){
    $(".trigger3").click(function(){
            var url=$(this).attr("url");
            $(".panel3").toggle("fast");
            $(this).toggleClass("active");
            return false;
    });
 });
 </script>

I have confirmed that the variable is there. 我已经确认变量在那里。

The second function is supposed to capture this variable and return it along with submitting form data. 第二个函数应该捕获此变量并将其与提交表单数据一起返回。

Here is the second function. 这是第二个功能。

 <script type="text/javascript">
 function submitForm2() {
    $.ajax({type:'POST', url: 'flagadd.php', data:$('#flagProxy').serialize()+'&url=url', success: function(response) {
    $('#flagProxy').find('.form_result2').html(response);
}});

return false;
 } 
 </script>


 <div class="panel3">
 <form id="flagProxy" onsubmit="return submitForm2();">
 <textarea style="resize:none" name="flag" cols="25" rows="5"></textarea>
 <input type="hidden" name="url" />
 <br>
 <input type="submit" name="submit" value="Submit" />
 <input type="button" name="cancel" value="Cancel" class="trigger3" />
 <div class="form_result2"></div>
 </form>
 </div>

I've been trying to figure this out for a few days but I'm not sure what to do. 我一直试图解决这个问题几天,但我不知道该怎么办。 I'm thinking either call one function from within a function or somehow make the variable global to both functions. 我想要么在一个函数中调用一个函数,要么以某种方式将变量全局化为两个函数。

Thanks for any help you can offer, it is much appreciated and I could use a good nights sleep :) 感谢您提供的任何帮助,非常感谢,我可以使用一个良好的夜晚睡觉:)

You can call one function from the other passing the value as a parameter but if both functions are event-triggered then I'll use a global variable to save that value: 您可以从另一个函数调用一个函数作为参数传递值,但如果两个函数都是事件触发的,那么我将使用全局变量来保存该值:

 <script type="text/javascript">

 var global;

 $(document).ready(function(){
    $(".trigger3").click(function(){
            var url=$(this).attr("url");
            $(".panel3").toggle("fast");
            $(this).toggleClass("active");
            global = 3;
            return false;
    });
 });

function submitForm2() {
    console.log(global); // 3
    $.ajax({type:'POST', url: 'flagadd.php', data:$('#flagProxy').serialize()+'&url=url', success: function(response) {
    $('#flagProxy').find('.form_result2').html(response);
}});

return false;
}

 </script>

Don't just make up HTML attribute names. 不要只是组成HTML属性名称。 If you want to store data, use the HTML5 data-* attributes—eg, data-url="..." instead. 如果要存储数据,请使用HTML5 data-*属性 - 例如, data-url="..."

If you just want to send it as part of a form, why not just have a hidden field in the form and update it? 如果您只想将其作为表单的一部分发送,为什么不在表单中隐藏字段并更新它?

<input type="hidden" name="url">

+ +

var url = $(this).data('url');  // jquery understands HTML5 data-* attributes too
$('#flagProxy input[name=url]').val(url);

Even better, instead of a link, just use a button inside the form with a value. 更好的是,只需使用值内的表单内的按钮,而不是链接。

<input type="submit" name="url" value="...">

(I'd use <button> , but older IE is really dumb and doesn't correctly handle sending its form value.) (我使用<button> ,但旧的IE实际上是愚蠢的,并没有正确处理发送其表单值。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM