Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Jquery通过选择字段进行表单验证_Jquery_Html_Forms - Fatal编程技术网

使用Jquery通过选择字段进行表单验证

使用Jquery通过选择字段进行表单验证,jquery,html,forms,Jquery,Html,Forms,当Jquery没有从评级表中选择任何值时,Jquery不会提醒我。 是否因为selected=selected实际上被视为jquery的一个值 <label for="rating">Rating</label> <select id="rating" name="rating" /> <option selected="selected" disabled="disabled">Select a Rating</

当Jquery没有从评级表中选择任何值时,Jquery不会提醒我。 是否因为selected=selected实际上被视为jquery的一个值

  <label for="rating">Rating</label>
    <select id="rating" name="rating" />
        <option selected="selected" disabled="disabled">Select a Rating</option>
        <option value="Terrible">Terrible</option>
        <option value="Fair">Fair</option>
        <option value="Ok">Ok</option>
        <option value="Good">Good</option>
        <option value="Excellent">Excellent</option>
       </select>


      $(document).ready(function(){

        // No value for movie_title
        if ($('#movie_title').val() == "" ) {
            alert ("No Film");
        }

        // No Value for actor
        if ($('#leading_name').val() == "") {
            alert ("No actor");
        }

        // No value for rating
        if ($('#rating').val() == "") {
            alert ("No Rating");
        }

        //No value for review
        if ($('#review').val() == "") {
            alert ("No review");
        }

    // Focus on first form field.
        $("input:text:visible:first").focus();

   })

这是因为如果没有指定值,默认值将是文本

将默认选项的值设为0,并检查:

<option selected="selected" value=0 disabled="disabled">Select a Rating</option>
您可以使用本机selectedIndex查找第一个选择的索引-

$'rating'.val返回空值:

添加值属性在Chrome中似乎不起作用,但检查null会起作用


哦,我选择了错误的id审查/评级。事实上这也是错误的。。。这是漫长的一天。我要指出的是:jquery有一些插件,比如jquery ui,它需要value属性exicient来处理这一问题,如果$'rating'.val==0{alert No rating;},您将如何检查该值。我从来不知道这样的网站存在。DOMLevel-1规范中的WickedIt-所选选项的顺序索引。如果未选择任何元素,则返回值-1。如果选择了多个选项,则返回第一个所选选项的索引。get0提供jQuery选择中的第一个原始dom元素-
if($('#rating').get(0).selectedIndex === 0){alert("first item")}
console.log($('#rating').val()); //null
console.log($('#rating').val() == "0"); //false
console.log($('#rating').val() == null); //true