我有这段代码:
<html>
<head>
<script type="text/javascript">
/***********************************************
* Drop Down Date select script- by JavaScriptKit.com
* This notice MUST stay intact for use
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and more
***********************************************/
var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
function populatedropdown(dayfield, monthfield, yearfield){
var today=new Date()
var dayfield=document.getElementById(dayfield)
var monthfield=document.getElementById(monthfield)
var yearfield=document.getElementById(yearfield)
for (var i=0; i<31; i++)
dayfield.options[i]=new Option(i, i+1)
dayfield.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true) //select today's day
for (var m=0; m<12; m++)
monthfield.options[m]=new Option(monthtext[m], monthtext[m])
monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true) //select today's month
var thisyear=1920; // fixed start year of 1900
var nowyear = 1994;
var diff = nowyear - thisyear +1; // number of years from 1900
for (var y=0; y<diff; y++){
yearfield.options[y]=new Option(thisyear, thisyear)
thisyear+=1
}
}
</script>
<script>
function getcombined(){
var year = document.getElementbyId("yeardropdown").value;
var month = document.getElementById("monthdropdown").value;
var day = document.getElementById("daydropdown").value;
var combineddob = year + "-" + "month" + "-" + day;
document.getElementById("hidden1").value=combineddob
}
</script>
</head>
<body>
<form action="" name="someform">
<select id="daydropdown" name='day' value="daydropdown">
</select>
<select id="monthdropdown" name='month' value="monthdropdown">
</select>
<select id="yeardropdown" name='year' value="yeardropdown">
</select>
<input type='hidden' id='hidden1' name='dob' value="" />
<input type='submit' />
</form>
<script type="text/javascript">
window.onload=function(){
populatedropdown("daydropdown", "monthdropdown", "yeardropdown")
}
</script>
</body>
</html>
提交表单时,我需要<input type='hidden' id='hidden1' name='dob' value='' />
更新为var combineddob
。
我尝试了几种方法,但我对javascript知之甚少,所以我对这些修复方法并不擅长。
我可能在寻找一些东西,但我还没有弄清楚这里有什么问题。
答案 0 :(得分:2)
如果您希望在用户提交表单之前发生某些事情,则应设置表单的onsubmit属性。在这种情况下:
<script>
<!--
function getcombined(){
var year = document.getElementById("yeardropdown").value;
var month = document.getElementById("monthdropdown").value;
var day = document.getElementById("daydropdown").value;
var combineddob = year + "-" + month + "-" + day;
document.getElementById("hidden1").value=combineddob;
return true;
}
-->
</script>
</head>
<body>
<form action="" name="someform" onsubmit='return getcombined();'>
我在函数中添加了“return true”,表示表单应该事后提交。我还修复了两个拼写错误,即getElementbyId中的小写字母b和缺少分号。
答案 1 :(得分:1)
更改你的HTML
<input type='submit' />
到这个
<input type='submit' id="submitButton" />
并在您的脚本中
window.onload = function(){
populatedropdown("daydropdown", "monthdropdown", "yeardropdown");
var submitBtn = document.getElementById("submitButton");
submitBtn.onsubmit = function(){getcombined();};
};
这将允许您在单击提交按钮时在onsubmit
中运行该功能。它将依次调用函数getcombined();
,然后提交表单。
修改强>
也许你应该改变这两行:
var combineddob = year + "-" + "month" + "-" + day;
document.getElementById("hidden1").value=combineddob
到
var combineddob = year + "-" + month + "-" + day;
document.getElementById("hidden1").value=combineddob;
和
var year = document.getElementbyId("yeardropdown").value;
to(注意getElementbyId
不是函数)
var year = document.getElementById("yeardropdown").value;
答案 2 :(得分:1)
首先:
var year = document.getElementbyId("yeardropdown").value;
为:
var year = document.getElementById("yeardropdown").value;
在提交按钮中添加onclick:
<input type='submit' onclick="getcombined();alert(hidden1.value);" />