English 中文(简体)
通行证
原标题:javascript form access
<form name = "myForm" onsubmit="foo()">
    <p class = "form-text">Name</p>
    <input type = "text" name="name" />
    <input type = "submit" />
</form>

//javascript (external file)
function foo(){
    alert(document.forms["myForm"].name.value)
}

该法典是否有任何错误? 当我报到时,它就跑到一个白页,没有发生。

预期成果:贴有名称的警示箱

最佳回答

你从未具体说明你想要取得什么成果,因此,我只能假定,你希望看到在提交表格时所警告的价值。

如果你must在网上使用javascript,你需要使用return false;,以防止提交表格,尽管我相信你与依赖<代码>foo(<>>>>>/code(return false)的人一样,可以使用return foo(>);,允许你选择是否防止发生这种事件:

onsubmit="return foo()"
///JS
function foo() {
  alert(...);
  return false;
}

类似守则的问题是,它直接依靠称为“联合材料”的超文本,从而打破了“超高频”和“ Java”结构。 相反,在《联合材料法》中添加事件提示:

//make sure you add an `[id]` to your form
var form = document.getElementsById( formid );
form.onsubmit = foo;

function foo() {
  //do stuff
  return false;
}
问题回答

尝试:

<form name = "myForm" onsubmit="foo();return false;">
    <p class = "form-text">Name</p>
    <input type = "text" name="name" />
    <input type = "submit" />
</form>

//javascript
function foo(){
    alert(document.forms["myForm"].name.value)
}

问题是,你没有阻止提交表格。 履行以下职能:根据您的意图(true>>_fal<>/code>(>true for “send it” and false for “don t sent it”)。 然后使用<代码>onsubmit=“return foo();,根据foo()的回报价值,直接影响表格的提交。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签