【问题标题】:Passing a form as an argument in javascript function在javascript函数中将表单作为参数传递
【发布时间】:2015-09-10 17:49:46
【问题描述】:

我正在尝试将表单作为参数传递给函数。这不是该页面上的唯一表单。我不确定这是否是传递表单并使用它的正确方法。这是我的代码 sn-p。

echo "<form action='/index.php/Taxbrowser_Taxonpage'  name='speciesSpecForm' id='speciesSpecForm' target='_blank'>";
echo '<table class="alignTop" width="700px"><tr><td>';
foreach ($ties as $tie) {
echo "<a href='/index.php/Taxbrowser_Taxonpage?taxon={$tie}' onclick='return waitingDialog(this.form,this.href)'><em>{$tie}</em></a></br>";
if ($i == $col1max || $i == $col2max) {
    echo '</td><td>';
}
$i++;
}
echo '</td></tr></table></form>';

而我的函数看起来是这样的,

function waitingDialog(form,url) {
        invokeAction(form, url, 'popup', 1024, 640, '', 'Tree Based Identification', 'Working', '');
        return false;
}

invokeAction 函数长这样,

function invokeAction(payload, destination, destinationType, popupWidth, popupHeight, windowName, title, message){

var toType = function(obj) {
     return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
}

//Since Javascript doesn't have default values this check sets default values if they are not present
payload = typeof payload != 'undefined' ? payload : '';
destination = typeof destination != 'undefined' ? destination : '';
destinationType = typeof destinationType != 'undefined' ? destinationType : 'nopopup';
popupHeight = typeof popupHeight != 'undefined' ? popupHeight : 1024;
popupWidth = typeof popupWidth != 'undefined' ? popupWidth : 768;
windowName = typeof windowName != 'undefined' ? windowName : 'popupWin';
title = typeof title != 'undefined' ? title : 'Loading';
message = typeof message != 'undefined' ? message : '';

if(title.length > 71){
    title = title.substring(0, 68) + "...";
}
if (destinationType == "nopopup"){
    showWaitingDialogBox(title,message);

    if (destination != ""){
        if (toType(payload) == 'object') {
            $(payload).attr('action', destination);
        } else {
            $('form[name="'+payload+'"]').attr('action', destination);
        }
    }
    if (toType(payload) == 'object') {
        $(payload).attr("target",'_self');
        $(payload).submit();
    } else {
        $('form[name="'+payload+'"]').attr('target', '_self');
        $('form[name="'+payload+'"]').submit();
    }
} else { //handles popup and others
    //check if a window with the same destinationType exists
    if (destinationType != null){
        destinationType = destinationType + "1";
    } 
    popup = window.open('/index.php/Working', destinationType,'scrollbars=yes,resizable=yes,width='+popupWidth+',height='+popupHeight);
    popup.focus();
    //e.preventDefault();
    setTimeout(function(){

        if (destination != ""){
            if (toType(payload) == 'object') {
                $(payload).attr('action', destination);
            } else {
                $('form[name="'+payload+'"]').attr('action', destination);
            }
        } 
        if (toType(payload) == 'object') {
            $(payload).attr("target",destinationType);
            $(payload).submit();
        } else {
            $('form[name="'+payload+'"]').attr('target', destinationType);
            $('form[name="'+payload+'"]').submit();
        }
    },
    450);
}

}

【问题讨论】:

  • 你为什么使用表格?没有输入?
  • @PiX06 对于“invokeAction”函数,参数中应该有一个形式。所以,它更像是一个虚拟形式。你能告诉我有什么替代方法吗?谢谢!
  • 你能把 invokeAction 函数的代码贴出来,让我们看看它对传递的参数做了什么吗?
  • @PiX06 我已经在问题中添加了。
  • @PiX06 只是想知道你能想出它不工作的原因吗?

标签: javascript php arguments forms


【解决方案1】:

查看此链接,它可以解决与您类似的问题。基本上,不能从不是表单元素的元素(在您的示例中为标签)引用 this.form 。您可以从其他输入元素中完成它。更好的方法是使用 getElementById()

https://www.webmasterworld.com/javascript/4294780.htm

【讨论】:

    【解决方案2】:

    发现了解决方案:

    echo "<a  onclick=\"waitingDialog($(this).parents('form'),'/index.php/Taxbrowser_Taxonpage?taxon={$tie}')\"><em>{$tie}</em></a></br>";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多