【问题标题】:How to change the value of hidden input field before submitting如何在提交前更改隐藏输入字段的值
【发布时间】:2015-07-20 11:48:23
【问题描述】:

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

【问题讨论】:

    标签: javascript jquery html forms


    【解决方案1】:

    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
    }
    

    【讨论】:

    • 也应该是document.getElementsByName('uri')[0].value 因为getElementsByName 返回一个集合
    【解决方案2】:

    您可以为按钮设置单击事件处理程序并将按钮类型更改为type="button",这样它就不会直接提交表单。内部点击处理程序将 balue 分配给 uri 输入,然后提交表单。

    HTML:

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

    jQuery:

    $(function(){
       $('#daily').click(function(){
          $('input[name="uri"]').val('androidinfodaily');
          window.open('https://feedburner.google.com/fb/a/mailverify?uri=androidinfodaily', 'popupwindow');
          //submit form
          $('#feedburner').submit();
       });
    
       $('#weekly').click(function(){
        $('input[name="uri"]').val('androidinfodaily');
          window.open('https://feedburner.google.com/fb/a/mailverify?uri=androidinfoweekly', 'popupwindow');
          //submit form
          $('#feedburner').submit();
      });
    });
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-03
        • 1970-01-01
        • 2018-06-07
        相关资源
        最近更新 更多