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

时间:2015-07-20 11:48:24

标签: javascript jquery html forms

我有一个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>

解决

我修复了我的代码,现在它可以运行了。这是最终的变体:

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

3 个答案:

答案 0 :(得分:5)

submit event在表单元素上触发,而不是在提交按钮上触发,因此请使用click处理程序

  

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

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

最好将脚本编写为类似

的函数
<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>

然后

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

答案 1 :(得分:2)

您可以为按钮设置点击事件处理程序,并将按钮类型更改为alfresco\web-client-application-context.xml,以便它不会直接提交表单。内部点击处理程序将balue分配给type="button"输入,然后提交表单。

HTML:

uri

jQuery的:

<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="button" value="Daily" id="daily"> 

    <input type="button" value="Weekly" id="weekly">

</form>

答案 2 :(得分:2)

不要忘记[0]第一个元素:

document.getElementsByName('uri')[0].value = 'androidinfodaily';