0

HTMLフォームにドロダウンボックスがあります。ドロップダウンから「0」が選択されたときにフォームをリセットする必要があります。それ以外の場合は、フォームに入力するために ajax 呼び出しを行います。その目的のために次のコードを書きましたが、機能していません。つまり、フォーム フィールドがリセットされていません。

$("select#student_id").change(function(){
  var student = $(this).val;
  if(student!=0)
    {
      //Here I make the ajax call to populate the form
    }

ここでフォームフィールドをリセットするにはどうすればよいですか。

4

5 に答える 5

1
$("select#student_id").change(function(){
  var student = $(this).val;
  if(student!=0)
    {
      //Here I make the ajax call to populate the form
    }
  else
  {
    var $form = $('#form_id_here');
    $form.find("input, textarea, hidden").val("").text("");
    $form.find("input[type='checkbox'], input[type='radio']").is(":checked").attr('checked', false);
  }
});
于 2013-09-18T11:20:07.147 に答える
1

リセットはjquery関数ではないため、dom要素でリセット関数を呼び出す必要があるため、機能しません

$('form')[0].reset()
于 2013-09-18T11:20:45.987 に答える
1
$("select#student_id").change(function(){
    var student = $(this).val();
    if(student!=0) {
        //make ajax call
    } else {
        $("form").reset();
    }
});
于 2013-09-18T11:19:24.163 に答える
0

ユーザーが「0」を選択したときに何が起こるかを言わなければなりません。

if(student!=0)
    {
      //Here I make the ajax call to populate the form
}
else{
 //reset form
}
于 2013-09-18T11:20:03.023 に答える
0
$( "#student_id" ).change(function(event, ui){
    if( $(this).val() != 0 )
    {
      //Here I make the ajax call to populate the form
      alert("Fill the form");
    }
    else {
        //reset all the fields of the form to the default values
        $(event.target.form)[0].reset();
    }
});
于 2013-09-18T11:42:51.203 に答える