在新标签中打开链接时,表单提交不会发生

时间:2017-02-06 09:22:47

标签: javascript html forms form-submit new-window

我有一个带有javascript提交功能的表单。 我有3个链接(锚标签)形式&想要根据点击的链接为提交的隐藏参数设置不同的值

表单提交通常可以正常工作,但如果我尝试在新选项卡中打开链接,则表单不会提交。 javascript函数中的警报是 没打印。有没有人有任何想法解决这个问题?

以下是示例代码:

<html>

    <head>
        <script type="text/javascript">
            function submitForm(actionParam) {
            alert("in submitForm");
            document.getElementById("action").value = actionParam;
            document.forms['myForm'].action = action;
            document.forms['myForm'].submit();
        }
        </script>
    </head>

    <body>

        <form name="myForm" id="myForm" method="post" action="/businesspanel">
            <input type="hidden" id="action" name="action" value="" />

            <a href="#" onclick="submitForm('action1');">Action 1</a></span>
            <a href="#" onclick="submitForm('action2');">Action 2</a></span>
            <a href="#" onclick="submitForm('action3');">Action 3</a></span>

        </form>

    </body>
</html>

2 个答案:

答案 0 :(得分:1)

在新窗口中打开链接会绕过JavaScript。不要使用链接/ JS来提交表单。

只需使用提交按钮即可。您甚至可以在不涉及JS的情况下发送您的识别值。

<button name="action" value="action1">Action 1</button>
<button name="action" value="action2">Action 2</button>
<button name="action" value="action3">Action 3</button>

答案 1 :(得分:0)

def integrator_func(y, t, q, m, n, d, k):
    y = np.copy(y.reshape((n*2,d)))

    # rj across, ri down
    rs_from = np.tile(y[:n], (n,1,1))

    # ri across, rj down
    rs_to = np.transpose(rs_from, axes=(1,0,2))

    # directional distance between each r_i and r_j
    # dr_ij is the force from j onto i, i.e. r_i - r_j
    dr = rs_to - rs_from

    # Used as a mask to ignore divides by zero between r_i and r_i
    nd_identity = np.eye(n).reshape((n,n,1))

    # WHAT I AM UNSURE ABOUT
    drmag = ma.array(
        np.power(
            np.sum(np.power(dr, 2), 2)
        ,3./2)
        ,mask=nd_identity)

    # Pairwise q_i*q_j for force equation
    qsa = np.tile(q, (n,1))
    qsb = np.tile(q, (n,1)).T
    qs = qsa*qsb

    # Directional forces
    Fs = (k*qs/drmag).reshape((n,n,1))

    # Dividing by m to obtain acceleration vectors
    a = np.sum(Fs*dr, 1)

    # Setting velocities
    y[:n] = np.copy(y[n:])

    # Entering the acceleration into the velocity slot
    y[n:] = np.copy(a)

    # Flattening it out for scipy.odeint to work properly
    return np.array(y).reshape(n*2*d)

def sim_particles(t, r, v, q, m, k=1.):
    """
    With n particles in d dimensions:

    t: timepoints to integrate over
    r: n*d matrix. The d-dimensional initial positions of n particles
    v: n*d matrix of initial particle velocities
    q: n*1 matrix of particle charges
    m: n*1 matrix of particle masses
    k: electric constant.
    """

    d = r.shape[-1]
    n = r.shape[0]
    y0 = np.zeros((n*2,d))
    y0[:n] = r
    y0[n:] = v
    y0 = y0.reshape(n*2*d)
    yf = odeint(
        integrator_func,
        y0,
        t,
        args=(q,m,n,d,k)).reshape(t.shape[0],n*2,d)
    return yf

“action”未在此处定义。如果您想在新标签页中打开链接,请执行以下操作:

document.forms['myForm'].action = action;