0

入力がぼやけているときにフォーマットマスクを与えるために入力を変更するjQueryを使用するプラグインがありますが、フォームが送信されたときにフォーマットされた値を送信したくありません。

親フォームの送信イベントを傍受し、要素の値を調整してマスクを削除し、プラグインで入力を使用するすべてのフォームに対して定義することなく送信を続行する方法はありますか?

もちろん、他のオプションは、非表示の入力を使用してその中に生の値を保存し、名前のない入力を使用してマスクを表示することですが、可能であれば余分なマークアップを避けたいです (さらに、便利なトリックです!)

4

2 に答える 2

1
$("#formid").submit(function(e){


    e.preventDefault(); //prevent the form from submitting

    var formatted_string = $("#formatted_input"); // get the formatted string you want to unformat

    var unformatted_string = formatted_string.replace(/regularexpression or string to be replaced/,'replacement string');  // use .replace() function to find and replace either a string or regular expression to undo your previous formatting

    $("#formatted_input").val(unformatted_string);       // set the input value as the new unformatted value

    $(this).submit(); //submit this form

});
于 2012-11-09T13:33:38.150 に答える
0

onsubmitフォームのイベントを使用できます。
このイベントは、フォームの送信イベントをインターセプトし、データを操作したり、フォームを送信するかどうか (リターン コードによって) を決定したりできます。

一般的な使用例の 1 つは検証です。

<script type="text/javascript">
function ValidateInput() {
  //validate something here
  return true; //true will submit the form, false will stop the submission here
}
</script>

<form action="yourfile.html" onsubmit="return ValidateInput();">
    ...
</form>

これを簡単に適応させてデータを操作できます。

于 2012-11-09T11:48:46.997 に答える