在javascript函数中传递一个表单作为参数

时间:2015-06-24 13:31:19

标签: javascript php arguments html-form

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

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);
}

}

2 个答案:

答案 0 :(得分:0)

Check this link which solves a problem similar to yours. Basically, this.form is can not be referenced from elements that are not form elements (in your example an tag). You could have done it from other input elements. A better way to approach will be to use getElementById() https://www.webmasterworld.com/javascript/4294780.htm

答案 1 :(得分:0)

发现了解决方案:

extension Car {
    func possibleColors() -> [String] {
        if let selfClass = self.dynamicType as? AnyClass {
            let bundle = NSBundle(forClass: selfClass)
            if let path = bundle.pathForResource("test", ofType: "colors") {
                if let keyValueList = NSDictionary(contentsOfFile: path) {
                    return keyValueList.allKeys as! [String]
                }
            }
        }

        return [String]()
    }
}