如何在提交前更改隐藏输入字段的值

Iur*_*lai 5 html javascript forms jquery

我有一个Feedburner订阅表,有两个按钮,一个用于每日新闻,一个用于每周新闻.问题是如何在提交之前更改名为'uri'的隐藏输入字段的值?我的解决方案不起作用.

这是我尝试使用的:

<form id="feedburner" action="https://feedburner.google.com/fb/a/mailverify"
    method="post" target="popupwindow">
    <p>
        <input autocomplete="off" value="Enter your email…"
            onblur="if (this.value == '') {this.value = 'Enter your email…';}"
            onfocus="if (this.value == 'Enter your email…') {this.value = '';}"
            type="text" name="email"/>
        <input type="hidden" name="uri"/>
        <input type="hidden" name="loc" value="en_US"/>
    </p>

    <input type="submit" value="Daily" onsubmit="document.getElementsByName('uri').value = 'androidinfodaily'; window.open('https://feedburner.google.com/fb/a/mailverify?uri=androidinfodaily', 'popupwindow'); return true" checked> 

    <input type="submit" value="Weekly" onsubmit="document.getElementsByName('uri').value = 'androidinfoweekly'; window.open('https://feedburner.google.com/fb/a/mailverify?uri=androidinfoweekly', 'popupwindow'); return true">

</form>
Run Code Online (Sandbox Code Playgroud)

解决了

我修复了我的代码,现在它可以工作了.这是最终的变体:

<form id="feedburner" action="https://feedburner.google.com/fb/a/mailverify"
    method="post" target="popupwindow">
    <p>
        <input autocomplete="off" value="Enter your email…"
            onblur="if (this.value == '') {this.value = 'Enter your email…';}"
            onfocus="if (this.value == 'Enter your email…') {this.value = '';}"
            type="text" name="email"/>
        <input type="hidden" name="uri" />
        <input type="hidden" name="loc" value="en_US"/>
    </p>

    <input type="submit" value="Daily" onclick="document.getElementsByName('uri')[0].value = 'androidinfodaily'; window.open('https://feedburner.google.com/fb/a/mailverify?uri=androidinfodaily', 'popupwindow'); return true"> 

    <input type="submit" value="Weekly" onclick="document.getElementsByName('uri')[0].value = 'androidinfoweekly'; window.open('https://feedburner.google.com/fb/a/mailverify?uri=androidinfoweekly', 'popupwindow'); return true">

</form>
Run Code Online (Sandbox Code Playgroud)

Aru*_*hny 5

提交事件的表单元素上发射,而不是提交按钮,所以用单击处理程序

请注意,仅在表单元素上触发提交,而不是按钮或提交输入.(提交表格,而不是按钮.)

<form id="feedburner" action="https://feedburner.google.com/fb/a/mailverify"
    method="post" target="popupwindow">
    <p>
        <input autocomplete="off" value="Enter your email…"
            onblur="if (this.value == '') {this.value = 'Enter your email…';}"
            onfocus="if (this.value == 'Enter your email…') {this.value = '';}"
            type="text" name="email"/>
        <input type="hidden" name="uri"/>
        <input type="hidden" name="loc" value="en_US"/>
    </p>

    <input type="submit" value="Daily" onclick="document.getElementsByName('uri')[0].value = 'androidinfodaily'; window.open('https://feedburner.google.com/fb/a/mailverify?uri=androidinfodaily', 'popupwindow'); return true" checked> 

    <input type="submit" value="Weekly" onclick="document.getElementsByName('uri')[0].value = 'androidinfoweekly'; window.open('https://feedburner.google.com/fb/a/mailverify?uri=androidinfoweekly', 'popupwindow'); return true">

</form>
Run Code Online (Sandbox Code Playgroud)

最好将脚本表示为类似的函数

<form id="feedburner" action="https://feedburner.google.com/fb/a/mailverify" method="post" target="popupwindow">
    <p>
        <input autocomplete="off" value="Enter your email…" onblur="if (this.value == '') {this.value = 'Enter your email…';}" onfocus="if (this.value == 'Enter your email…') {this.value = '';}" type="text" name="email" />
        <input type="hidden" name="uri" />
        <input type="hidden" name="loc" value="en_US" />
    </p>
    <input type="submit" value="Daily" onclick="return beforeSubmit('androidinfodaily')" checked />
    <input type="submit" value="Weekly" onclick="return beforeSubmit('androidinfoweekly')" />
</form>
Run Code Online (Sandbox Code Playgroud)

然后

function beforeSubmit(type) {
    document.getElementsByName('uri')[0].value = type;
    window.open('https://feedburner.google.com/fb/a/mailverify?uri=' + type, 'popupwindow');
    return true
}
Run Code Online (Sandbox Code Playgroud)