他在这里错过了什么? [英] What the he** am I missing here?

查看:78
本文介绍了他在这里错过了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function SetDefaultDate(){

d = new Date();

return d;

}

........

< TD align = left>开始日期:< / TD>

< TD align = left> < SELECT name =" batchStartDate"大小= QUOT 1 QUOT; maxlength =" 50"

value ="< SCRIPT> SetDefaultDate()< / SCRIPT>">

< / TD>

< / TR>

我只想将html控件默认为今天的日期。


为什么这一切都如此痛苦...... ....也许是改变职业生涯的时间:(

function SetDefaultDate() {
d = new Date();
return d;
}
........
<TD align=left> Start Date: </TD>
<TD align=left> <SELECT name="batchStartDate" size="1" maxlength="50"
value="<SCRIPT>SetDefaultDate()</SCRIPT>">
</TD>
</TR>
I just want to default the html control to todays date.

Why is this all so painful......maybe time for a career change :(

推荐答案




Brent Eamer是一个ecrit:


Brent Eamer a ecrit :

函数SetDefaultDate(){
d = new Date();
返回d;
}
...... 。
< TD align = left>开始日期:< / TD>
< TD align = left>< SELECT name =" batchStartDate" size =" 1" maxlength =" 50"
value ="< SCRIPT> SetDefaultDate()< / SCRIPT>">


可能是......?


value ="< SCRIPT> document.write(SetDefaultDate())< / SCRIPT>">

< / TD>
< / TR>

function SetDefaultDate() {
d = new Date();
return d;
}
.......
<TD align=left> Start Date: </TD>
<TD align=left> <SELECT name="batchStartDate" size="1" maxlength="50"
value="<SCRIPT>SetDefaultDate()</SCRIPT>">
may be ... ?

value="<SCRIPT>document.write(SetDefaultDate())</SCRIPT>">
</TD>
</TR>



布伦特Eamer于2003年12月18日在星期四,星期四,13:11:36 GMT写道:
Brent Eamer wrote on 18 Dec 2003 at Thu, 18 Dec 2003 13:11:36 GMT:
function SetDefaultDate(){
d = new Date();
返回d;
}
.......
< TD align = left>开始日期:< / TD>
< TD align = left> < SELECT name =" batchStartDate" size =" 1"
maxlength =" 50" value ="< SCRIPT> SetDefaultDate()< / SCRIPT>">
< / TD>
< / TR>
function SetDefaultDate() {
d = new Date();
return d;
}
.......
<TD align=left> Start Date: </TD>
<TD align=left> <SELECT name="batchStartDate" size="1"
maxlength="50" value="<SCRIPT>SetDefaultDate()</SCRIPT>">
</TD>
</TR>




我要说:那个SCRIPT块被解释为它应该是在那个上下文中的
:CDATA(字符串)值。但是当我检查HTML标准时(检查值属性包含

CDATA),我意识到SELECT元素没有值属性;

选定的OPTION元素是SELECT的值。 SELECT

元素也没有maxlength属性。基本上,即使你的HTML语法的脚本部分是正确的,也就是
,这不是,

以上仍然无法正常工作。


如果不确切知道你想要实现什么,我就不能给你一个明确的回答,但是我可以指出你的权利

方向。


正如我已经说过的那样,你对HTML的期望是*从不*

将会发生。这就像指定一个IMG元素作为

a BUTTON的值并期望它呈现图像:


< BUTTON value ="< ; IMG src =''someImage.gif''>">< / BUTTON> [1]


有两种方法可以实现您希望实现的目标:


1)使用document.write写入整个元素( ),将日期连接成固定字符串,或者;

2)在创建元素之后的某个时刻将值赋给元素

(例如,当onload事件被触发时)。


我将在下面假设您在HTML中想要一个下拉框

显示,作为它的初始选项,仅当前本地日期(不是

时间)。


第一种方法的示例:


< SELECT name =" batchStartDate" size =" 1">

< SCRIPT type =" text / javascript">

//请注意,您忘了在脚本中指定脚本类型

//示例。


var now = new Date();


document.write(

''< OPTION selected>''+ now.getFullYear()+'' - ''

+ now.getMonth()+'' - ''+ now。 getDate()+''< \ / OPTION>'');


//生成HTML:< OPTION selected> YYYY-M-d< / OPTION> < br $>
//

//其中YYYY是四个字母的年份

// M是月份(没有前导零)

// d是当天(没有前导零)

< / SCRIPT>

< / SELECT>

一个例子对于第二种方法:


< HEAD>

<! - 为onload设置默认脚本语言

以下活动 - >

< META http-equiv =" Content-Script-Type"

content =& text / javascript">


< SCRIPT type =" text / javascript">

function insertCurrentDate(){

var now = new Date();

var value = now.getFullYear()+'' - ''+ now.getMonth()+'' - ''

+ now.getDate();

var initial = null;


//找到OPTION元素并替换它的显示文字和

//到当前日期的值

if(document.getElementById){

var initial = document.getElementById(''initial' ');

}否则if(document.all){

var initial = document.all [''initial''];

}

if(initial){

initial.text = value;

initial.value = value;

}

}

< / SCRIPT>

...

< / HEAD>


< BODY onload =" insertCurrentDate()">

...

< SELECT name =" batchStartDate" ; size =" 1">

< OPTION id =" initial"选择>今天< / OPTION>

...

< / SELECT>


我*认真*推荐你刷新你的HTML知识和

JavaScript。


Mike

[1]正确的语法当然是:


< BUTTON>< IMG src =" someImage.gif">< / BUTTON>


-

Michael Winter
M。****** @ blueyonder.co.inva lid(将.invalid替换为.uk)



I was going to say: "That SCRIPT block is being interpreted as it''s
supposed to be in that context: a CDATA (string) value." But when I
checked the HTML standard (to check that value attributes contain
CDATA), I realised that SELECT elements don''t have value attributes;
the selected OPTION elements are the values for the SELECT. SELECT
elements don''t have a maxlength attribute, either. Basically, even
if the script part of your HTML syntax was correct, which it isn''t,
the above still wouldn''t work.

Without knowing exactly what you were trying to acheive, I can''t
give a definitive answer, but I can point you in the right
direction.

As I already said, what you are expecting from that HTML is *never*
going to happen. It''s like specifying an IMG element as the value of
a BUTTON and expecting it to render the image:

<BUTTON value="<IMG src=''someImage.gif''>"></BUTTON> [1]

There are two methods of doing what you hope to acheive:

1) Write the entire element using document.write(), concatenating
the date into a fixed string, or;
2) Assign the value to the element at some point after creating it
(when the onload event is fired, for example).

I''ll assume below that in your HTML you wanted a drop-down box that
displayed, as it''s initial option, the current local date only (not
time).

An example for the first method:

<SELECT name="batchStartDate" size="1">
<SCRIPT type="text/javascript">
// Notice that you forgot to specify the script type in your
// example.

var now = new Date();

document.write(
''<OPTION selected>'' + now.getFullYear() + ''-''
+ now.getMonth() + ''-'' + now.getDate() + ''<\/OPTION>'' );

// Produces the HTML: <OPTION selected>YYYY-M-d</OPTION>
//
// where YYYY is a four letter year
// M is the month (no leading zero)
// d is the day (no leading zero)
</SCRIPT>
</SELECT>
An example for the second method:

<HEAD>
<!-- Set the default script language
for the onload event below -->
<META http-equiv="Content-Script-Type"
content="text/javascript">

<SCRIPT type="text/javascript">
function insertCurrentDate() {
var now = new Date();
var value = now.getFullYear() + ''-'' + now.getMonth() + ''-''
+ now.getDate();
var initial = null;

// Find the OPTION element and replace it''s display text and
// value to the current date
if (document.getElementById) {
var initial = document.getElementById(''initial'');
} else if (document.all) {
var initial = document.all[''initial''];
}
if (initial) {
initial.text = value;
initial.value = value;
}
}
</SCRIPT>
...
</HEAD>

<BODY onload="insertCurrentDate()">
...
<SELECT name="batchStartDate" size="1">
<OPTION id="initial" selected>Today</OPTION>
...
</SELECT>

I *seriously* recommend that you refresh your knowledge of HTML and
JavaScript.

Mike
[1] The correct syntax is, of course:

<BUTTON><IMG src="someImage.gif"></BUTTON>

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk")


文章< cI ************** ****** @ursa-nb00s0.nbnet.nb.ca>,
be **** @ islandtelecom.com 启发我们...
In article <cI********************@ursa-nb00s0.nbnet.nb.ca>,
be****@islandtelecom.com enlightened us with...
function SetDefaultDate(){
d = new Date();
return d;
}
。 ......
< TD align = left>开始日期:< / TD>
< TD align = left> < SELECT name =" batchStartDate"大小= QUOT 1 QUOT; maxlength =" 50"
value ="< SCRIPT> SetDefaultDate()< / SCRIPT>">
< / TD>
< / TR>

我只是想将html控件默认为今天的日期。

为什么这一切都如此痛苦......也许是改变职业生涯的时间:(
function SetDefaultDate() {
d = new Date();
return d;
}
.......
<TD align=left> Start Date: </TD>
<TD align=left> <SELECT name="batchStartDate" size="1" maxlength="50"
value="<SCRIPT>SetDefaultDate()</SCRIPT>">
</TD>
</TR>
I just want to default the html control to todays date.

Why is this all so painful......maybe time for a career change :(




因为当你选择

元素有选项和selectedIndex,而不是值时,你试图设置一个值为select元素。你也是当它不是选择元素的属性时,给它

它是一个maxlength。选择

元素有选项。选项有值。

选择一个选项使用你的脚本(你需要一个循环或者你需要

需要知道选项索引来设置它),然后调用

中的脚本onLoad of body。请注意,如果用户

在离开页面后按下后退按钮,这将会搞砸,因为日期将是

重置而不是保持正如用户期望的那样。为了解决这个问题,我先检查

的值,然后在我的东西中重置它。

如果这是一个文本元素,你仍然应该使用
document.formname.elementname.value = d;

不要尝试在价值报价内调用上面的脚本。

工作正常与服务器端<%= val%>类型语句,但我已经看到它从未在客户端使用脚本块。


-

-

~kaeli~

是否有可能完全偏袒?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace



Because you''re trying to set a value to a select element when select
elements have options and selectedIndex, not values. You''re also giving
it a maxlength when that is not a property of select elements. Select
elements have options. Options have values.
Choose an option with your script (you''ll either need a loop or you''ll
need to know the option index to set it), then call the script in the
onLoad of the body. Note that this will muck things up if the user
presses the back button after leaving the page, as the date will be
reset instead of holding on as the user expects. To help this, I check
for a value before resetting it in my stuff.
If this were a text element, you should still set in the script by using
document.formname.elementname.value=d;
not try to call a script like you did above inside the value quotes.
That works fine with server-side <%= val %> type statements, but I''ve
never seen it used client-side with a script block.

--
--
~kaeli~
Is it possible to be totally partial?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace


这篇关于他在这里错过了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆