0

我有一个名为保存更改的函数,它看起来像这样

function saveChanges() {
  var theCity = city.value;
  var theState = state.value;
  var theEmail = email.value;
  var theAge = age.value;
  var theGender = gender.value;
  var theOther = other.value;
    alert("theCity is: "+ theCity);
    alert("theState is: "+ theState);
    alert("theEmail is: "+ theEmail);
    alert("theAge is: "+ theAge);
    alert("theGender is: "+ theGender);
    alert("theOther is: "+ theOther);
}
window.onload=function (){
    document.getElementById('ok').onclick=saveChanges;

}

我有一个简单的 html 页面,上面有多个输入。

要提交内容,我在底部有一个链接。

<a href="#" id="ok" style="color:white;">Click here <-- </a>

我制作了一个具有相同 id='ok' 的按钮,但它不起作用。

我错过了什么?

4

5 回答 5

1

请试试这个,

HTML

<form onsubmit="return validate(this)" method="POST" action="a_post_page.php">
    name: <input type="text" name="name" /> <br />
    email: <input type="text" name="email" /> <br />
    age: <input type="text" name="age" /> <br />
    gender: <input type="text" name="gender" /> <br />
    city: <input type="text" name="city" /> <br />
    <br />
    <input type="submit" name="submit_form" value="Save" />
</form>

Javascript

function validate(form1) {  
    if(form1.name.value === "") {  
        alert("Please type your name the name feild.");  
        return false;  
    }  
    if(form1.email.value === "") {  
        alert("Please type your email the email feild.");  
        return false;  
    }  
    if(form1.age.value === "") {  
        alert("Please type your age the age feild.");  
        return false;  
    }  
    if(form1.gender.value === "") {  
        alert("Please type your gender the gender feild.");  
        return false;  
    }  
    if(form1.city.value === "") {  
        alert("Please type your city the city feild.");  
        return false;  
    }  
    return true;  
}  
于 2012-12-17T06:17:19.393 回答
0

尝试

HTML

<input type='text' id='city'> <br />
  <input type='text' id='state'> <br />
  <input type='text' id='email'> <br />
  <input type='text' id='age'> <br />

<hr>
<a href="#" id="ok" style="">Click here </a>​​​​​​​​​​​​​​​​

Javascript

function id(e){
   return document.getElementById(e);
}

function saveChanges(){
   var city  = id('city').value,
       state = id('state').value,
       email = id('email').value,
       age   = id('age').value;

    alert("City : " + city + "\nState :" + state  +"\nEmail : " + email + "\n Age  : " + age);  

    return false;
}
window​.onload = function(){    
    document.getElementById("ok").onclick = saveChanges;
};​

演示:http: //jsfiddle.net/Zjrd3/

于 2012-12-17T04:43:47.710 回答
0

我假设您的保存功能会检查表单中的某些字段以提交它们。如果是这样,那么错误与链接或提交无关,问题在于您使用变量的方式。你不能只做city.value,你需要使用document.getElementById('city').value。这将导致 javascript 给你一个“变量未定义”错误并停止执行。正确的代码是:

function saveChanges() {
  var theCity = document.getElementById('city').value;
  var theState = document.getElementById('state').value;
  var theEmail = document.getElementById('email').value;
  var theAge = document.getElementById('age').value;
  var theGender = document.getElementById('gender').value;
  var theOther = document.getElementById('other').value;
    alert("theCity is: "+ theCity);
    alert("theState is: "+ theState);
    alert("theEmail is: "+ theEmail);
    alert("theAge is: "+ theAge);
    alert("theGender is: "+ theGender);
    alert("theOther is: "+ theOther);
}
window.onload=function (){
    document.getElementById('ok').onclick=saveChanges;

}
于 2012-12-17T04:28:19.130 回答
0

document.getElementById不会适用于多个元素。ID 应该是唯一的。

于 2012-12-17T04:45:54.850 回答
0

href 是同一页。在运行该脚本之前,单击该按钮可能会重定向。尝试返回 false 或其他东西。

于 2012-12-17T04:20:53.547 回答