Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.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/2/jquery/75.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
Javascript 按必需属性验证输入元素,并获取每个输入的标签文本_Javascript_Jquery - Fatal编程技术网

Javascript 按必需属性验证输入元素,并获取每个输入的标签文本

Javascript 按必需属性验证输入元素,并获取每个输入的标签文本,javascript,jquery,Javascript,Jquery,表单中有一些HTML代码,我需要检查带有required=“required”属性的字段是否为空。大部分HTML代码如下所示: <div class="p_name"> <label class="required" for="product_name">Name</label> <input type="text" maxlength="50" required="required" name="product[name]" id="p

表单中有一些HTML代码,我需要检查带有
required=“required”
属性的字段是否为空。大部分HTML代码如下所示:

<div class="p_name">
    <label class="required" for="product_name">Name</label>
    <input type="text" maxlength="50" required="required" name="product[name]" id="product_name">
</div>

表单元素由一些具有
id
的节元素包装,例如
#product-create-step-3
,因此根据节元素是什么,我应该只检查活动节中的字段,由于我不知道如何获取标签文本,有人能在这段代码上给我一些帮助吗?

如果您所有字段的HTML结构与您发布的相同,您可以使用,
.prev()


如果所有字段的HTML结构与您发布的相同,则可以使用,
.prev()

您可以将选择器与一起使用来提取文本,因为
label
元素是相关输入元素的前一个同级元素

$('input[required="required"]').each(function() {
    if ($(this).val().length === 0) {
        // Here I should get the text of the label element for 
        // each input and write in the alert but don't know how
        alert($(this).prev().text() + ' cannot be blank');
    }
});
您可以将选择器与一起使用来提取文本,因为
label
元素是相关输入元素的前一个同级元素

$('input[required="required"]').each(function() {
    if ($(this).val().length === 0) {
        // Here I should get the text of the label element for 
        // each input and write in the alert but don't know how
        alert($(this).prev().text() + ' cannot be blank');
    }
});

$('input[required="required"]').each(function() {
    if ($(this).val().length === 0) {
        // Here I should get the text of the label element for 
        // each input and write in the alert but don't know how
        alert($(this).prev().text() + ' cannot be blank');
    }
});
$('input[required="required"]').each(function() {
    if ($(this).val().length === 0) {
        var lbl = $(this).siblings('label');
        //or var lbl = $(this).prev('label');
        alert(lbl.text());
    }
});