最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return false if no value is selected (other than heading) from select box - Stack Overflow

programmeradmin0浏览0评论

I have a HTML form having select box. On selection of first drop down, next drop down should be auto filled using AJAX.

On Download Records (id="getCsv") button click event a CSV file is generated. Problem is, I want to make all the fields mandatory. Here is the jquery code

var teacher_name = $("#sel_teacher option:selected").text();
var unittest_name = $("#sel_test1 option:selected").text();

var class_name = $("#sel_class1 option:selected").text();
var class_id = $('#sel_class1').val();

var division_name = $("#sel_div1 option:selected").text();
var division_id = $('#sel_div1').val();

var subject_name = $("#sel_sub1 option:selected").text();

if (teacher_name == "") {
    alert('Please Select Teacher Name.');
    return false;
} else if(class_name == "") {
    alert('Please Select Class Name.');
    return false;
} else if(division_name == "") {
    alert('Please Select Division Name.');
    return false;
} else if(subject_name == "") {
    alert('Please Select Subject Name.');
    return false;
} else if(unittest_name == "") {
    alert('Please Select Unit Test Name.');
    return false;
} else {
    var myObject = new Object();
    myObject.class_name = class_name;
    myObject.class_id = class_id;
    myObject.division_name = division_name;
    myObject.division_id = division_id;
    myObject.subject_name = subject_name;
    myObject.test_name = unittest_name;

    var formData = JSON.stringify(myObject);
    $('#getCsv').attr('href','csv_generator.php?data=' + formData);
}

The problem is that when I click Download Records, even though the first select box is empty directly alert box for second select box pops up. I tried to solve this problem using the below, but no luck.

if ($("#sel_teacher").attr("selectedIndex") == 0) {
   alert("You haven't selected anything!");
   return false;
}

Can anybody please help me with this? Any help is appreciated.

I have a HTML form having select box. On selection of first drop down, next drop down should be auto filled using AJAX.

On Download Records (id="getCsv") button click event a CSV file is generated. Problem is, I want to make all the fields mandatory. Here is the jquery code

var teacher_name = $("#sel_teacher option:selected").text();
var unittest_name = $("#sel_test1 option:selected").text();

var class_name = $("#sel_class1 option:selected").text();
var class_id = $('#sel_class1').val();

var division_name = $("#sel_div1 option:selected").text();
var division_id = $('#sel_div1').val();

var subject_name = $("#sel_sub1 option:selected").text();

if (teacher_name == "") {
    alert('Please Select Teacher Name.');
    return false;
} else if(class_name == "") {
    alert('Please Select Class Name.');
    return false;
} else if(division_name == "") {
    alert('Please Select Division Name.');
    return false;
} else if(subject_name == "") {
    alert('Please Select Subject Name.');
    return false;
} else if(unittest_name == "") {
    alert('Please Select Unit Test Name.');
    return false;
} else {
    var myObject = new Object();
    myObject.class_name = class_name;
    myObject.class_id = class_id;
    myObject.division_name = division_name;
    myObject.division_id = division_id;
    myObject.subject_name = subject_name;
    myObject.test_name = unittest_name;

    var formData = JSON.stringify(myObject);
    $('#getCsv').attr('href','csv_generator.php?data=' + formData);
}

The problem is that when I click Download Records, even though the first select box is empty directly alert box for second select box pops up. I tried to solve this problem using the below, but no luck.

if ($("#sel_teacher").attr("selectedIndex") == 0) {
   alert("You haven't selected anything!");
   return false;
}

Can anybody please help me with this? Any help is appreciated.

Share Improve this question edited Jan 4, 2017 at 11:21 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Aug 3, 2015 at 13:24 Parag JadhavParag Jadhav 1,8992 gold badges25 silver badges42 bronze badges 3
  • Please show relevant HTML. If you give your select options a value, and the empty one value="" then $("#sel_teacher.val() will give you a proper string to test – mplungjan Commented Aug 3, 2015 at 13:30
  • 1 Try to change .text() for .val() var teacher_name = $("#sel_teacher option:selected").val(); – bicho Commented Aug 3, 2015 at 14:56
  • 1 @mplungjan I didnt post HTML code because I think a simple image would be helpful to understand the question better – Parag Jadhav Commented Aug 4, 2015 at 2:59
Add a ment  | 

3 Answers 3

Reset to default 3

selectedIndex is a property, use prop:

$("#sel_teacher").prop("selectedIndex")

Also, you can simplify your code by retrieving the selected value using just $("#sel_teacher").val() and pare to empty string (assuming the value of that option is empty).

var teacher_name = $("#sel_teacher").val();
// get other <select /> values here...

if (teacher_name == '') {
   alert("You haven't selected anything!");
   return false;
}

// test other values here...

It might be because of the default value that you have given for the first text-box.Just change the value to "" onclick or on blur on that text-box.

Or you can simply handle this matter via HTML5 attribute required and adding onchange() Event Listener .

<select name="sel_teacher" onchange="get_value();" id="sel_teacher" required>
 <option>--Select Teacher Name--</option> 

</select>

<script>
 function get_value() {
   var teacher_name = $("#sel_teacher").val();
  // get other <select /> values here...

   if (teacher_name == '') {
     alert("You haven't selected anything!");
     return false;
   } else {

     // write code when teacher_name is selected

   }
 }
</script>
发布评论

评论列表(0)

  1. 暂无评论