0

名前とメールアドレスの必須フィールドを含む 2 つのフォームを備えたページがあります。どちらのフォームにも一意の ID と名前があり、必須フィールドにも一意の名前が付けられています。ドキュメント ヘッドに 2 セットの ValidateRequiredFields スクリプトがあり、2 番目のスクリプトが適用されるフォームは、最初のスクリプトが適用されるフォームの必須フィールドにも入力が必要であることを示しています。スクリプトは次のとおりです。

    <script type="text/javascript" language="JavaScript">
<!-- Copyright 2005 Bontrager Connection, LLC
//
// Two places need to be customized.
//
//
// Place 1:
// Between the quotation marks, specify the name of 
//    your form.

var FormName = "theForm2";


// Place 2:
// Between the quotation marks, specify the field names 
//    that are required. List the field name separated 
//    with a comma.

var RequiredFields = "FullName,EmailAddress";


//
// No other customization of this JavaScript is required.
//
/////////////////////////////////////////////////////////

function ValidateRequiredFields()
{
var FieldList = RequiredFields.split(",")
var BadList = new Array();
for(var i = 0; i < FieldList.length; i++) {
    var s = eval('document.' + FormName + '.' + FieldList[i] + '.value');
    s = StripSpacesFromEnds(s);
    if(s.length < 1) { BadList.push(FieldList[i]); }
    }
if(BadList.length < 1) { return true; }
var ess = new String();
if(BadList.length > 1) { ess = 's'; }
var message = new String('\n\nThe following field' + ess + ' are required:\n');
for(var i = 0; i < BadList.length; i++) { message += '\n' + BadList[i]; }
alert(message);
return false;
}

function StripSpacesFromEnds(s)
{
while((s.indexOf(' ',0) == 0) && (s.length> 1)) {
    s = s.substring(1,s.length);
    }
while((s.lastIndexOf(' ') == (s.length - 1)) && (s.length> 1)) {
    s = s.substring(0,(s.length - 1));
    }
if((s.indexOf(' ',0) == 0) && (s.length == 1)) { s = ''; }
return s;
}
// -->


</script>




<script type="text/javascript" language="JavaScript">
<!-- Copyright 2005 Bontrager Connection, LLC
//
// Two places need to be customized.
//
//
// Place 1:
// Between the quotation marks, specify the name of 
//    your form.

var FormName = "theForm";


// Place 2:
// Between the quotation marks, specify the field names 
//    that are required. List the field name separated 
//    with a comma.

var RequiredFields = "Name,Email";


//
// No other customization of this JavaScript is required.
//
/////////////////////////////////////////////////////////

function ValidateRequiredFields()
{
var FieldList = RequiredFields.split(",")
var BadList = new Array();
for(var i = 0; i < FieldList.length; i++) {
    var s = eval('document.' + FormName + '.' + FieldList[i] + '.value');
    s = StripSpacesFromEnds(s);
    if(s.length < 1) { BadList.push(FieldList[i]); }
    }
if(BadList.length < 1) { return true; }
var ess = new String();
if(BadList.length > 1) { ess = 's'; }
var message = new String('\n\nThe following field' + ess + ' are required:\n');
for(var i = 0; i < BadList.length; i++) { message += '\n' + BadList[i]; }
alert(message);
return false;
}

function StripSpacesFromEnds(s)
{
while((s.indexOf(' ',0) == 0) && (s.length> 1)) {
    s = s.substring(1,s.length);
    }
while((s.lastIndexOf(' ') == (s.length - 1)) && (s.length> 1)) {
    s = s.substring(0,(s.length - 1));
    }
if((s.indexOf(' ',0) == 0) && (s.length == 1)) { s = ''; }
return s;
}
// -->


</script>

各フォームのコードの最初の数行は次のとおりです。

フォーム1

     <form action="contactengine_required.php" method="post" name="theForm2" id="theForm2" onsubmit="return ValidateRequiredFields();" >
 <input class="span7" type="text" name="FullName" id="FullName" placeholder="Full Name (required)" />
  <input class="span7" type="text" name="EmailAddress" id="EmailAddress" placeholder="Email (required)" />

フォーム2

         <form action="contactengine_required_footer.php" method="post" name="theForm" id="theForm" onsubmit="return ValidateRequiredFields();" >
                                    <div class="clearfix">
                                        <label for="Name"><span style="color: #FFF; font-size: 12px">*Name:</span></label>
      <input class="span4" type="text" name="Name" id="Name"/>



                                    </div>

                                    <div class="clearfix">
                                        <label for="Email"><span style="color: #FFF; font-size: 12px">*Email:</span></label>

開発ページはこちら。これを整理するための助けをいただければ幸いです。

4

1 に答える 1

0

コード ブロックの 2 番目のコピーをファイルに配置すると、最初のコピーが上書きされます。そのコード ブロックを 2 回使用する場合は、2 つのバージョンが同じグローバル変数と同じ関数名を使用しないように、少し変更する必要があります。( ValidateRequiredFieldsFormNameおよびRequiredFieldsあなたの場合)

コピーしたコードの 2 番目のインスタンスを次のように置き換えます。

var FormName2 = "theForm";
var RequiredFields2 = "Name,Email";

function ValidateRequiredFields2()
{
var FieldList = RequiredFields2.split(",")
var BadList = new Array();
for(var i = 0; i < FieldList.length; i++) {
    var s = eval('document.' + FormName2 + '.' + FieldList[i] + '.value');
    s = StripSpacesFromEnds(s);
    if(s.length < 1) { BadList.push(FieldList[i]); }
    }
if(BadList.length < 1) { return true; }
var ess = new String();
if(BadList.length > 1) { ess = 's'; }
var message = new String('\n\nThe following field' + ess + ' are required:\n');
for(var i = 0; i < BadList.length; i++) { message += '\n' + BadList[i]; }
alert(message);
return false;
}

そして、2番目のフォームONSUBMITをに変更しますonsubmit="return ValidateRequiredFields2();"

于 2013-05-18T00:07:32.343 に答える