2015-07-20 71 views
2

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

UPDATE

感謝所有收到的答覆我有固定我的代碼,現在它的工作原理。這是最後的變種:

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

回答

4

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> 

它也將更好地speprate腳本函數像

<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 
} 
+0

也儘量http://jsfiddle.net/arunpjohny/bedsaagk/2/ –

+1

另外它應該是'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(); 
    }); 
}); 
1

沒有爲第一要素忘記[0]

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