根据选择字段更新文本字段 [英] update text field based on selection field
问题描述
下午。
我在HTML页面上有两个字段。 HTML看起来像这样...
< html>
< head>
< title>新的第3页< ; / title>
< / head>
< body>
< form method =" POST" action =" index.asp">
< select size =" 1"名称= QUOT; shiptype" onchange =" crazyJavascriptFuntion()">
<选择的选项> SELECT ONE< / option>
< option value =" 3"> GROUND (3天)< / option>
< option value =" 2"> 2ND DAY AIR(2天)< / option>
<选项值=" 1"> OVERNIGHT(1天)< / option>
< / select>
< input type =" text"名称= QUOT; DUEDATE" size =" 20">< / p>
< / form>
< / body>
< / html>
我希望文本字段自动更新到今天的日期
+从船型中选择适当的选项。
所以,如果有人选择GROUND,名为duedate的文本框将
更新至今天+ 1天。
任何对此的帮助将不胜感激。
pk*******@hotmail.com 写道:
我希望文本字段能够自动更新到今天的日期
+适当的选择来自船型。
一个简单的方法是......
< script type =" text / javascript">
function setDueDate(oSel){
var nNow = new Date()。getTime(),
nOff = oSel.options [oSel.selectedIndex] .value * 8.64E7,
// 86400000毫秒是一天
dDue =新日期(nNow + nOff);
oSel.form.elements [" duedate"]。value
= dDue.getFullYear()+" /"
+ digits(dDue.getMonth()+ 1,2)+" /"
+位数(dDue.getDate(),2);
}
函数挖它的(s,n){
s = s.toString();
while(s.length< n)s =0 + s;
返回s;
}
< / script>
[...]
< select name =" shiptype" onchange =" setDueDate(this)">
....但这取决于客户系统的正确日期设置。
如果您的Web服务器支持Apache服务器端包含(SSI),
您可以使用一个小的SSI扩展来避免这种情况:
<! - #config timefmt ="%a,%d%b%Y%T GMT">
< script type =" text / javascript">
var timeOffset = new Date()。getTime()
- 新日期(<! - #echo var =" DATE_GMT" - >")。getTime( );
函数getServerTime(){
返回新日期(新日期()。getTime() - timeOffset);
}
函数setDueDate(oSel){
var nNow = getServerTime(),
nOff = oSel.options [oSel.selectedIndex] .value * 8.64E7 ,
dDue =新日期(nNow + nOff);
oSel.form.elements [" duedate"]。value
= dDue .getFullYear()+" /"
+ digits(dDue.getMonth()+ 1 ,2)+" /"
+ digits(dDue.getDate(),2);
}
函数数字(s ,n){
s = s.toString();
while(s.length< n)s =0 + s;
返回s;
}
< / script>
[...]
< select name =" shiptype" onchange =" setDueDate(this)">
ciao,dhgm
Dietmar Meier写道:
[...]函数数字(s,n){
s = s.toString();
while(s.length< n) s =0 + s;
返回s;
}
这可能更简单:
//将前导零加到小于10的数字
函数位数(x){
return(x <10)? x =0 + x:x;
}
-
Fred
< blockquote> Fred Oz写道:
function digits(s,n){
s = s.toString();
而(s.length< n)s =" 0" + s;
返回s;
}
这可能更简单:
是(但是数字(s,n) )是多功能的。
//将前导零加到小于10的数字上。
函数数字(x){
返回(x <10)? x =0 + x:x;
^^^^
删除x =。
}
另一个简单而快速的方法:
函数digits2(s){
return(Number(s)+ 100).toString()。substring(1) ;
}
ciao,dhgm
ok, this is gonna seem stupid, but it has been stumping me all
afternoon.
I have two fields on a HTML page. HTML looks like this...
<html>
<head>
<title>New Page 3</title>
</head>
<body>
<form method="POST" action="index.asp">
<select size="1" name="shiptype" onchange="crazyJavascriptFuntion()">
<option selected>SELECT ONE</option>
<option value="3">GROUND (3 DAY)</option>
<option value="2">2ND DAY AIR (2 DAY)</option>
<option value="1">OVERNIGHT (1 DAY)</option>
</select>
<input type="text" name="duedate" size="20"></p>
</form>
</body>
</html>
I would like for the text field to automatically update to todays date
+ appropriate selection from ship type.
so if someone selects "GROUND" the text box called "duedate" will
update to Today + 1 day.
any help on this would be greatly appreciated.
pk*******@hotmail.com wrote:
I would like for the text field to automatically update to todays date
+ appropriate selection from ship type.
A simple approach is ...
<script type="text/javascript">
function setDueDate(oSel) {
var nNow = new Date().getTime(),
nOff = oSel.options[oSel.selectedIndex].value * 8.64E7,
// 86400000 milliseconds are one day
dDue = new Date(nNow + nOff);
oSel.form.elements["duedate"].value
= dDue.getFullYear() + "/"
+ digits(dDue.getMonth() + 1, 2) + "/"
+ digits(dDue.getDate(), 2);
}
function digits(s, n) {
s = s.toString();
while (s.length < n) s = "0" + s;
return s;
}
</script>
[...]
<select name="shiptype" onchange="setDueDate(this)">
.... but this depends on correct date settings at client''s system.
If your web server supports apache style Server Side Includes (SSI),
you can simply avoid that using a litte SSI extension:
<!--#config timefmt="%a, %d %b %Y %T GMT"-->
<script type="text/javascript">
var timeOffset = new Date().getTime()
- new Date("<!--#echo var="DATE_GMT" -->").getTime();
function getServerTime() {
return new Date(new Date().getTime() - timeOffset);
}
function setDueDate(oSel) {
var nNow = getServerTime(),
nOff = oSel.options[oSel.selectedIndex].value * 8.64E7,
dDue = new Date(nNow + nOff);
oSel.form.elements["duedate"].value
= dDue.getFullYear() + "/"
+ digits(dDue.getMonth() + 1, 2) + "/"
+ digits(dDue.getDate(), 2);
}
function digits(s, n) {
s = s.toString();
while (s.length < n) s = "0" + s;
return s;
}
</script>
[...]
<select name="shiptype" onchange="setDueDate(this)">
ciao, dhgm
Dietmar Meier wrote:
[...]function digits(s, n) {
s = s.toString();
while (s.length < n) s = "0" + s;
return s;
}
This can be a tad simpler:
// Add leading zero to numbers less than 10
function digits(x) {
return (x<10)? x = "0" + x : x;
}
--
Fred
Fred Oz wrote:
function digits(s, n) {
s = s.toString();
while (s.length < n) s = "0" + s;
return s;
}This can be a tad simpler:
Yes (but digits(s, n) is more versatile).
// Add leading zero to numbers less than 10
function digits(x) {
return (x<10)? x = "0" + x : x; ^^^^
Drop that "x = ".
}
Another simple and fast one:
function digits2(s) {
return (Number(s) + 100).toString().substring(1);
}
ciao, dhgm
这篇关于根据选择字段更新文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!