0

我有一个表单,其中包含一些简单的 javascript 来验证一个简单的问卷。它在表单中的输入上使用了 name 属性,但现在该页面已与客户端 CMS 集成,它需要使用我无法使用的特定命名约定。

旧表单的输入如下: <input type="text" name="firstName" />

但是对于 cms,所有字段都需要"fields[firstName]",例如: <input name="fields[first-name]">

我怎样才能让它工作。

现有的JS如下:

function validate_form ( )
{
    valid = true;

    if ( ( document.register.question[0].checked == false ) && ( document.register.question[1].checked == false ) && ( document.register.question[2].checked == false ) )
    {
        alert ( "Please choose an answer" );
        valid = false;
    }

    else if ( document.register.form_title.value == "" )
    {
        alert ( "Please fill in the 'Title' box." );
        valid = false;
    }

    else if ( document.register.fName.value == "" )
    {
        alert ( "Please add your First Name" );
        valid = false;
    }

    else if ( document.register.lName.value == "" )
    {
        alert ( "Please add your Last Name" );
        valid = false;
    }

    else if ( document.register.email.value == "" )
    {
        alert ( "Please add a valid email address" );
        valid = false;
    }

    else if ( document.register.country.selectedIndex == 0 )
    {
        alert ( "Please select your Country" );
        valid = false;
    }

    else if ( document.register.dob1.selectedIndex == 0 )
    {
        alert ( "Please ensure your date of birth is complete" );
        valid = false;
    }
    else if ( document.register.dob2.selectedIndex == 0 )
    {
        alert ( "Please ensure your date of birth is complete" );
        valid = false;
    }
    else if ( document.register.dob3.selectedIndex == 0 )
    {
        alert ( "Please ensure your date of birth is complete" );
        valid = false;
    }

    else if ( document.register.terms.checked == false )
    {
        alert ( "Please check the Terms & Conditions box." );
        valid = false;
    }


    return valid;
}
4

1 回答 1

0

您可以将 jquery 与以下内容一起使用:

$("input[name=fields\\[first-name\\]]").val()
于 2013-11-06T23:20:13.690 回答