【问题标题】:how to call a function with parameter in jquery, if function is in parent div如果函数在父div中,如何在jquery中调用带参数的函数
【发布时间】:2011-05-04 15:40:11
【问题描述】:

我想用一些参数调用一个函数,但该函数在 3 或 4 个父 div 中。我怎么能这么叫。

$("#bottom_bar #backbtn").click(function(){
        var $bottomLineBtn = $(this);
        $bottomLineBtn.parent().parent().parent().load_form('1', '55', 'update');

})

。 ...在那个目标位置...像根div ... parent.parent.parent.div(顺便说一句,我知道有多少父母)...编写了以下函数。

function load_form(list_index, record_id, form_action){
list_index = Number(list_index);
alert(list_index +" | "+ record_id +" | "+form_action);
}   

请帮帮我...谢谢请帮忙

【问题讨论】:

  • 通常你有 onSomeEvent="Handler" 用于控件/DOM。您可以使用 .trigger("event") 触发该事件,也有一些快捷方式 (.click())。
  • “函数在 3 或 4 个父 div 中”是什么意思?你能发布那个html片段吗?
  • @morgar 我已经简化了我的问题.. 请看这个.. stackoverflow.com/questions/5887126/…
  • 仍然不知道为什么要调用 jquery 链中的函数而不是简单地使用函数名称,即仅load_form('1', '55', 'update');

标签: jquery function parameters root


【解决方案1】:

嗯,我想这就是你需要的:

$("#bottom_bar #backbtn").click(function(){
        var $bottomLineBtn = $(this);
        //Pass the div as an argument
        load_form('1', '55', 'update', $bottomLineBtn.parent().parent().parent());

})

function load_form(list_index, record_id, form_action, div){
  //Here you have a reference to the div in the 'div' var, if you need it
  list_index = Number(list_index);
  alert(list_index +" | "+ record_id +" | "+form_action);
} 

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2016-11-13
    • 2019-11-29
    • 2020-09-20
    • 2020-02-27
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多