选择html标签默认选项标签选择错误打印

时间:2017-11-03 04:20:00

标签: php html

我需要当用户没有选择任何数据库值选项标签打印错误颜色到父div。我使用<input type="text"> html标记代码的代码来打印文本字段模式,不匹配时间打印错误。代码在下面

<label class="col-md-3 control-label">Product Title</label>
    <div class="col-md-6">
        <input type="text" name="product_title" class="form-control" required 
placeholder="Product Name" value="" pattern="([A-z0-9À-ž\s]){2,}" 
oninvalid="setCustomValidity('Please enter least two characters long name')"
onblur="this.parentNode.parentNode.style.backgroundColor=/^([A-z0-9À-ž\s]){2,}/.test
(this.value)?'inherit':'orange'" >
    </div>

这是为我onblur事件做的。 image of working print error 然后,当用户没有在<select></select>标签中选择任何数据库值选项时,我想要打印错误颜色。这是我给出的默认值。 <option>Select a Manufacturer</option>用户选择此项时我需要打印错误,或忘记在<option>标记中选择数据库值。

这是代码部分

<div class="form-group"> <!-- form-group Start -->
 <label class="col-md-3 control-label"> Select a Manufacturer </label>
    <div class="col-md-6">
    <select class="form-control" name="manufacturer" required="required">
        <option>Select a Manufacturer</option>
            <?php 

            $get_manufacturer ="select * from manufactures";
            $run_manufacturer = mysqli_query($con, $get_manufacturer);
                while($row_manufacturer = mysqli_fetch_array($run_manufacturer)){
    $manufacturer_id = $row_manufacturer['manufacturer_id'];
    $manufacturer_title = $row_manufacturer['manufacturer_title'];

echo "<option value='$manufacturer_id'>$manufacturer_title</option>";
        }
        ?>
        </select>
</div>

我该怎么做?

2 个答案:

答案 0 :(得分:0)

<option value="">Select a Manufacturer</option>

请参阅下一个链接以供参考:https://www.w3schools.com/tags/att_select_required.asp

答案 1 :(得分:0)

我在这里使用J-Query我的代码

<select class="form-control" name="manufacturer" required="required" onchange="getval(this);">
  <option value="0">Select a Manufacturer</option>

然后我用这个脚本来改变背景

<script type="text/javascript">
    function getval(sel)
    {
        if(sel.value == "0"){
            $("#sel").css("background-color", "orange");
        }
    }
</script>

最后我修好了 fixed code output