これまでのところうまく機能しているajaxform()プラグインを使用しています。しかし、私の入力フィールドにはデフォルト値があり、ユーザーが手を触れていないフォームを送信しただけの場合は、beforeSubmit:コールバックを使用してフォームを送信する前に空白にする必要があります。jquery ajaxform()で各フォームの入力を確認して、デフォルト値を空白にしようとしています
簡単に言えば、フォームの入力フィールドを確認し、必要に応じて送信を停止する構文はわかりません。私はeach()メソッドとthis.defaultValueを使用し、おそらくfalseを返すという考えを持っていますか?しかし、私は詳細については分かりません。
誰かがおそらく私にアイデアを与えることができますか?ありがとう。これまでのところ私のコードでは、checkValues()関数が残っています。
$(document).ready(function(){
//========= Functions =========
function styleForm() {
$('.quickcontact label').hide();
$('input[type="text"],textarea').addClass("idleField");
$('input[type="text"],textarea').focus(function() {
$(this).removeClass("idleField").addClass("focusField");
if (this.value == this.defaultValue){
this.value = '';
}
if(this.value != this.defaultValue){
this.select();
}
});
$('input[type="text"],textarea').blur(function() {
$(this).removeClass("focusField").addClass("idleField");
if ($.trim(this.value) == ''){
this.value = (this.defaultValue ? this.defaultValue : '');
}
});
}
//options for ajaxform() function
var options = {
target: '.quickcontactDisplay', // target element(s) to be updated with server response
beforeSubmit: checkValues, // pre-submit callback
success: reBind // post-submit callback
// other available options:
//url: url // override for form's 'action' attribute
//type: type // 'get' or 'post', override for form's 'method' attribute
//dataType: null // 'xml', 'script', or 'json' (expected server response type)
//clearForm: true // clear all form fields after successful submit
//resetForm: true // reset the form after successful submit
// $.ajax options can be used here too, for example:
//timeout: 3000
};
//rebinds the ajax functionality to updated form html
function reBind() {
// re-do the form, as it has just been replaced
$('form.quickcontact').ajaxForm(options);
styleForm();
}
//checks for default values of form on submit to prevent them being submitted
function checkValues(){
}
// ==== logic =====
$('form.quickcontact').ajaxForm(options);
styleForm();
});
そして、私のフォームのhtml:
<form action="/enquiries/add" method="post" id="EnquiryAddForm" class="quickcontact">
<input type="hidden" value="POST" name="_method"/>
<input type="hidden" id="EnquiryVisitorId" value="276" name="data[Enquiry][visitor_id]"/>
<input type="text" id="EnquiryName" maxlength="200" value="Your name" name="data[Enquiry][name]"/>
<input type="text" id="EnquiryEmailAddress" maxlength="200" value="Your Email" name="data[Enquiry][emailAddress]"/>
<textarea id="EnquiryEnquiry" rows="6" cols="30" name="data[Enquiry][enquiry]">Your Email Address</textarea>
<input type="submit" value="Ok, I'm done"/>
</form>
公正なポイント、再考のための時間。 –
透明なフィールドとラベルの絶対的な位置を使ってラベルをフィールドの下に置くことができます(そしてテキストが入力されたときにJSでclassNamesをトグルする)。ある時点でテクニックの書きかけを終えるまで私は周りを歩かなければならない。 – Quentin