1

我有这个代码:

<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 了解不多,所以我不擅长这些修复。

我可能正在寻找一些东西,但我还没有弄清楚这里有什么问题。

4

3 回答 3

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 和缺少的分号。

于 2012-10-01T23:58:55.210 回答
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;
于 2012-10-01T23:45:25.230 回答
1

首先:

var year = document.getElementbyId("yeardropdown").value;

至:

var year = document.getElementById("yeardropdown").value;

在提交按钮中添加 onclick:

<input type='submit' onclick="getcombined();alert(hidden1.value);" />
于 2012-10-01T23:53:28.167 回答