【问题标题】:How to call function inside Javascript如何在Javascript中调用函数
【发布时间】:2016-10-19 05:14:23
【问题描述】:
<script type="text/javascript>
function submitMe() {
    var checked_ids = [];
    $('#your-tree-id').jstree('get_checked',null,true).each(function(){
        checked_ids.push(this.id);
    });
    //setting to hidden field
    document.getElementById('jsfields').value = checked_ids.join(',');
}
</script>
<input type="hidden" name="jsfields" id="jsfields" value="">

我正在寻找在表单提交中检查 Jstree 的 ID 的方法。所以这就是我得到的。但是,如何在我的程序中调用此函数?

【问题讨论】:

  • 你想什么时候调用这个函数?
  • 你想从提交按钮/javascript的其他部分调用submitMe函数吗?请在问题中提及。
  • 只需将您的 submitMe 功能添加到您的表单 onsubmit 事件
  • @dex 是的,我想从提交按钮调用它
  • @guradio 我想在单击提交按钮时调用该函数

标签: javascript jquery checkbox jstree


【解决方案1】:

使用点击/提交事件

$(form).submit(submitMe);

$('[type="submit"]').click(submitMe);

不要忘记阻止默认事件,然后在代码之后触发它:

function submitMe(e) {
    e.preventDefault();
    var checked_ids = [];
    $('#your-tree-id').jstree('get_checked',null,true).each(function(){
        checked_ids.push(this.id);
    });
    //setting to hidden field
    document.getElementById('jsfields').value = checked_ids.join(',');
   window.location = $(this).closest('form').submit();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-11
    • 2019-04-04
    • 2017-05-28
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多