将表单变量传递给onsubmit字段?

时间:2013-07-10 19:45:51

标签: javascript forms nan onsubmit

我正在尝试在发送之前验证表单的内容。基本上,我正在尝试使用表单中的数字并确保它们在正确的范围内。问题是我试图验证它的JavaScript认为传递给它的项目是NaN(我一直在解析它)。

一个小小的工作揭示了变量(“大小”)指的是“HTMLInputEleMent”,我想这确实是NaN(虽然我不太确定它实际上是什么)。我认为问题是onSubmit没有传递我希望它传递的内容,即使我将字段命名为“size”并且我也传递了提交“大小”。

我尝试将它放在引号中,但这只是把它变成一个字符串......

我想知道您是否无法将表单中的WITHIN变量传递给它的onSubmit字段?是这样吗?如果是这样,我该怎么做?

以下是表格:

        <form onsubmit="return goodForm(size, day, month, year)" action="http://localhost:8080/pomper_servlet/CostCalc" method="GET">              
            The day of the month must be entered as a number (ex: 1,22)
            <input type="text" name="day"><br>
            The month of the year must be entered as a number (ex: Jan.=1, etc.)
            <input type="text" name="month"><br>
            The year must be entered as a 4 digit number (ex: 2008, 2017)
            <input type="text" name="year"><br>
            Please Choose a tour-length, in accordance with the chart below:
            <input type="TEXT" name="length"><br>
            How many people will be in your group? (No More than 10 allowed!)
            <input type="text" name="size"><br>                
            Please select a tour:<br>
            <input type="RADIO" name="tour" value="Gardiner Lake">
            Gardiner Lake<br>
            <input type="RADIO" name="tour" value="Hellroaring Plateau">
            Hellroaring Plateau<br>
            <input type="RADIO" name="tour" value="The Beaten Path">
            The Beaten Path<br>
            <input type="SUBMIT" value="Submit">
        </form>

这是函数,来自functions.js:

function goodForm(gSize, day, month, year) {
"use strict";
window.alert("goodFrame(): "+gSize);
var groupSize1 = parseInt( gSize.replace(/^"|"$/g, ""), 10);
window.alert("goodFrame(): "+groupSize1);
var sizeInt = parseInt(groupSize1);
if(groupSize(sizeInt) && goodDate(day, month, year)){
    window.alert("true");
    return true;
}
else{
    window.alert("false")
    return false;
}

其中有其他功能的参考,但我认为它们与此无关。警报是用于调试目的......

提前致谢!

4 个答案:

答案 0 :(得分:4)

首先,像这样(通过onsubmit)进行内联验证是不好的形式。通常你会想要进行事件绑定,我将使用jQuery包含示例代码,但你也可以使用其他方法。

首先,为表单提供页面的唯一ID属性。我假设<form id="MyForm"...

接下来,您可能希望验证方法“了解”所需的字段。

//this function is executed when the page's dom is loaded
// assumes jQuery is loaded already
$(function(){

    //binds the myFormOnSubmit method below to run as part of your form's onsubmit method
    $('#MyForm').submit(myFormOnSubmit);

    //runs when the form is trying to submit
    function myFormOnSubmit(event) {
        var f = $(this);

        // note, you have to match on attribute selectors
        //  you may want to give each of these fields an id=".." attribute as well to select against #IdName
        var size = f.find('[name=size]').val();
        var day = f.find('[name=day]').val();
        var month = f.find('[name=month]').val();
        var year = f.find('[name=year]').val();
        var tour = f.find('[name=tour]:checked').val(); //selected radio button's

        var isValid = validDate(year,month,day) && validSize(gSize) && validTour(tour);

        if (!isValid) {
            event.preventDefault(); //stop submit
        }
    }

    function validTour(tour) {
        return !!tour; //will be false if it's an empty string, ex: no selected value
    }

    function validSize(size) {
        var s = parseInt(size); //get integer value for size

        if (s <= 0 || s > 10) return false; //not in range
        if (s.toString() !== size) return false; //doesn't match input, invalid input
        return true; //true
    }

    function validDate(year, month, day) {
        //coerce the values passed into numbers
        var y = +year, m = +month, d = +day;

        //convert to an actual date object
        var dtm = new Date(y, --m, d);

        //compare the values
        if (!dtm) return false; //invalid input
        if (dtm.getFullYear().toString() !== year.toString()) return false; //year doesn't match input
        if ((dtm.getMonth() + 1).toString() !== month.toString()) return false; //month doesn't match input
        if (dtm.getDate().toString() !== day.toString()) return false; //day doesn't match input

        var now = new Date(); console.log(now);
        var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());

        //entered date is before today, invalid
        if (dtm <= today) return false;

        //passed checks
        return true;
    }
});

答案 1 :(得分:3)

这是什么意思?

JavaScript的:

 document.getElementById("myForm").onsubmit = function() {
     alert(document.getElementById("size").value);
 }

HTML:

<form name="myForm" id="myForm">
    <input type="text" name="size" id="size">
    <input type="submit">
</form>

精化:

onsubmit函数附加到id为“myForm”的项目,该项目在HTML中指定为id =“myForm”。您可以使用文档上的getElementById方法查找具有此ID的项目。小心不要做getElementByID(Id vs ID)。当您提交表单时,此方法将被调用,您将在途中。

然后,您可以在页面上查找项目,以获得与查找表单相同的值。只需给他们一个id =“size”的ID就可以查找了。

您还可以执行以下操作:

alert(document.myForm.size.value);

alert(document.forms["myForm"].size.value);

...但是我已经远离那种方法,因为不久前,至少有些浏览器讨厌它。也许它现在更好,更高效,我不确定。

答案 2 :(得分:3)

您可以尝试将每个输入(日,月,年,大小)赋予id(您可以使用与name属性相同的值)。 在goodForm()函数中按ID获取值。

<input type="text" name="day" id="day">&lt; - set

document.getElementById("some id").value&lt; - get

答案 3 :(得分:2)

如果您不想使用JQuery:

你不需要传递参数,尝试给它们一个id,并通过良好的表单函数中的id获取它们。

function goodForm() {
    var size= document.getElementById("size").value;
    if(size...){
    ...
    }

}
相关问题