问题
I have a Feedburner subscription form with two buttons, one for daily news and one for weekly news. The question is how to change the value of hidden input field with name 'uri' before submitting? My solution doesn't work.
This is what I try to use:
<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>
Solved
I have fixed my code and now it works. This is the final variant:
<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>
回答1:
The submit event is fired on the form element, not on submit button, so use click handlers
Note that submit is fired only on the form element, not the button or submit input. (Forms are submitted, not buttons.)
<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>
Also it will be better to speprate the script to a function like
<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>
then
function beforeSubmit(type) {
document.getElementsByName('uri')[0].value = type;
window.open('https://feedburner.google.com/fb/a/mailverify?uri=' + type, 'popupwindow');
return true
}
回答2:
You can have click event handler for button and change type of button to type="button" so that it will not submit the form directly. Inside click handler assign balue to uri input and then submit the form.
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:
do not forget [0] for the first element:
document.getElementsByName('uri')[0].value = 'androidinfodaily';
来源:https://stackoverflow.com/questions/31515734/how-to-change-the-value-of-hidden-input-field-before-submitting