2012-07-04 68 views
0

我有一個包含提交按鈕的表單。我正在使用form.validation插件兩種不同的表單驗證

我已經設置了規則。沒關係!

但現在我創建了一個鏈接表單,當用戶點擊時,我想改變這些規則。可能嗎?

例如:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script> 
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script> 
<script type="text/javascript"> 
$().ready(function() { 
    $(".myform").validate({ 
     rules: { 
      name: { 
       required: true 
      }, 
      email: { 
       required: true 
      }, 
     } 
    }); 
}); 
</script> 
<form class="myform"> 
    Name: <input type="text" name="name"> 
    Email: <input type="text" name="email"> 
    Phone: <input type="text" name="phone"> 
    <input type="submit" value="Send"> 
</form> 
<a href="#">Call me</a> 

當上「叫我」的用戶點擊,確認是只有「手機」領域。

+0

不知道'form.validation'插件是什麼。 – PeeHaa

+1

你至少可以發佈一些代碼或描述你實際嘗試過的東西嗎? –

+0

http://bassistance.de/jquery-plugins/jquery-plugin-validation/ –

回答

2

有很多不對的代碼都語法和結構

<script type="text/javascript"> 
$().ready(function() { 
$(".myform").validate({ 
    rules: { 
     name: { 
      required: true 
     }, 
     email: { 
      required: true 
     }, 
    } 
}); 
}); 
</script> 
<form class="myform"> 
Name: <input type="text" name="name"> 
Email: <input type="text" name="email"> 
Phone: <input type="text" name="phone"> 
</form> 
<a href="#">Call me</a> 

應該看起來更像

<script type="text/javascript"> 
$(document).ready(function() { 
$("#callme").click(function(){ 
    $(".myform").validate({ 
    rules: { 
     phone: { 
      required: true 
     } 
    /*note the removed ,*/ 
    } 
}); 
}); 
$("#submit").click(function(){ 
$(".myform").validate({ 
    rules: { 
     name: { 
      required: true 
     }, 
     email: { 
      required: true 
     } 
    /*note the removed ,*/ 
    } 
}); 
}); 
}); 
</script> 
<!-- Your Form must have a method attribute either post or get //--> 
<form class="myform" method="post" action=""> 
<label for="name">Name:</label> <input type="text" name="name" id="name"> 
<label for="email">Email:</label> <input type="text" name="email" id="email"> 
<label for="phone">Phone:</label> <input type="text" name="phone" id="phone"> 
<!-- Hide this label with css //--> 
<label for="submit" class="hidden">submit:</label><input type="submit" value="submit" text="submit" id="submit" /> 
</form> 
<!-- Link this to another form with new js validation rules //--> 
<a href="#" id="callme">Call me</a> 
+0

對不起,我知道你向我展示的所有錯誤,我的表單有很多字段,我只適應在這裏問stackoverflow。所以我認爲不必提供所有這些細節。但我已經編輯過了。 Tha的形式必須相同,這是我的問題,可能嗎? –

+0

這是,但你需要明白你寫的是什麼 –

+0

啊好的。我明白。 –

3

在我看來,你需要做的是點擊您的鏈接時,下列(全部在javascript/jQuery中):

  • 取消鏈接的默認動作;
  • nameemail中刪除規則;
  • 將新規則添加到phone字段;
  • 可以隱藏除phone字段以外的所有字段。

您可以在插件的文檔的rules section中找到更多詳細信息。

+0

簡單得多按住事件監聽鏈接和按鈕並修改設置事件中的驗證規則。否則規則在代碼中丟失 –

+0

@Nicholas King在這種情況下不起作用(根據手冊),「validate'方法'設置事件處理程序的提交,焦點,鍵入,模糊和點擊觸發驗證整個形式或個別元素「。因此,如果在事件已經被解僱後將其綁定,那就太遲了。 – jeroen

+0

只是閱讀文檔,但我很確定我的方法應該有點玩,並使用有效的方法,而不是驗證。否則,它使用像你一樣的插件規則()方法的案例建議 –

1

這種方式奏效。在我的測試中,我看到:

  • 不能是鏈接
  • Cannou出的

有人可以證實?

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script> 
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
$("#submit1").click(function(){ 
    $(".myform").validate({ 
    rules: { 
     phone: { 
      required: true 
     } 
    } 
}); 
}); 
$("#submit").click(function(){ 
$(".myform").validate({ 
    rules: { 
     name: { 
      required: true 
     }, 
     email: { 
      required: true 
     } 
    } 
}); 
}); 
}); 
</script> 
<!-- Your Form must have a method attribute either post or get //--> 
<form class="myform" method="post" action=""> 
<label for="name">Name:</label> <input type="text" name="name" id="name"> 
<label for="email">Email:</label> <input type="text" name="email" id="email"> 
<label for="phone">Phone:</label> <input type="text" name="phone" id="phone"> 
<!-- Hide this label with css //--> 
<input type="submit" value="submit" text="submit" id="submit" /> 
<input type="submit" value="submit1" text="submit" id="submit1" /> 
</form> 
<!-- Link this to another form with new js validation rules //--> 
+0

此代碼與下面的代碼完全相同。錨點完全有能力提交表單。 –

+0

此代碼基於你的,但你沒有工作......我只是不明白爲什麼。 –

+0

我認爲這是一個可能的解決方案,使用settings數組雖然這似乎是一個常見問題! http://stackoverflow.com/questions/1510165/how-can-i-add-remove-or-swap-jquery-validation-rules-from-a-page –