简体   繁体   English

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

[英]Passing a form as an argument in javascript function

I am trying to pass a form as an argument in a function.我正在尝试将表单作为函数中的参数传递。 This is not the only form on that page.这不是该页面上的唯一表单。 I am not sure if this is the right way to pass the form and consume it.我不确定这是否是传递表单并使用它的正确方法。 Here is my code snippet.这是我的代码片段。

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>';

And my function looks like this,我的功能看起来像这样,

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

invokeAction function looks like this, 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);
}

} }

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).基本上, this.form 不能从不是表单元素的元素(在你的例子中是一个标签)中引用。 You could have done it from other input elements.您可以从其他输入元素完成它。 A better way to approach will be to use getElementById()更好的方法是使用 getElementById()

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

发现解决办法:

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

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

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