使用Jquery Name属性从动态生成的元素中检索值

时间:2022-11-27 18:27:43

I have a mvc cshtml form where I am generating multiple rows using following syntax :

我有一个mvc cshtml表单,我使用以下语法生成多行:

foreach (var i in Model)
   {            
    <td>
     @Html.TextBoxFor(a => a[j].SFDCID, new { id = "SFDCID", @class ="SFDCID" })
    </td>
   }

This is resulting HTML snippet like :

这是生成的HTML代码段,如:

 <input class="SFDCID" id="SFDCID" name="[0].SFDCID" type="text" value="">
    <input class="SFDCID" id="SFDCID" name="[1].SFDCID" type="text" value="">
.....
.....

Now I want to check if user has entered values in all SFDCID field then only the form submit should happen else should throw validation message.

现在我想检查用户是否在所有SFDCID字段中输入了值,然后只有表单提交应该发生,否则应抛出验证消息。

For that I am trying to access the value using its name attribute & have written following code:

为此我尝试使用其name属性访问该值并编写以下代码:

           for (var i = 0; i <= totalNumberOfRows; i++) {

                var elementName = '[' + i + '].SFDCID';    
                if ($("[name = elementName]").val() == "" || 
                                     $("[name = elementName]").val() == undefined) 
                {
                        ConsolidatedErrorMsg = "SFDC ID is a Mandatory field.";
    }
}

I have tried with class, id & name as selector to get the value, but in first 2 case its only working for 1st row. With Name selector its working for both name if I hard code the element name like '[0].SFDCID' & '[1].SFDCID' but when I am substituting i with 0 & 1, its not working.

我已尝试使用class,id和name作为选择器来获取值,但在前2个案例中,它仅适用于第1行。使用名称选择器,如果我硬编码元素名称,如'[0] .SFDCID'和'[1] .SFDCID',则它适用于这两个名称,但是当我用0和1代替i时,它不起作用。

Kindly help me to rectify any syntax error I have in this selector.

请帮助我纠正我在此选择器中的任何语法错误。

1 个解决方案

#1


0  

$("[name = elementName]") is actually selecting all elements with the name of, literally, elementName. You actually need to escape square brackets and concat the value you have constructed:

$(“[name = elementName]”)实际上是选择名称为elementName的所有元素。实际上你需要转义方括号并连接你构造的值:

var elementName = '\\[' + i + '\\].SFDCID';
$("[name='" + elementName + "']")

Even better approach would be to use ends with selector:

更好的方法是使用带选择器的结尾:

$("[name$='\\.SFDCID']").each(function() {
    // this here refers to the found element
    var $this = $(this);
    if ($this.val() === "" || typeof($this.val()) === undefined) {
        ConsolidatedErrorMsg = "SFDC ID is a Mandatory field.";
    }
})

A couple of side notes. Please remove id = "SFDCID" bit, as duplicated IDs on the page may lead to inconsistent behavior. And consider using built-in ways to validate required fields.

几个旁注。请删除id =“SFDCID”位,因为页面上的重复ID可能会导致行为不一致。并考虑使用内置方法来验证必填字段。

#1


0  

$("[name = elementName]") is actually selecting all elements with the name of, literally, elementName. You actually need to escape square brackets and concat the value you have constructed:

$(“[name = elementName]”)实际上是选择名称为elementName的所有元素。实际上你需要转义方括号并连接你构造的值:

var elementName = '\\[' + i + '\\].SFDCID';
$("[name='" + elementName + "']")

Even better approach would be to use ends with selector:

更好的方法是使用带选择器的结尾:

$("[name$='\\.SFDCID']").each(function() {
    // this here refers to the found element
    var $this = $(this);
    if ($this.val() === "" || typeof($this.val()) === undefined) {
        ConsolidatedErrorMsg = "SFDC ID is a Mandatory field.";
    }
})

A couple of side notes. Please remove id = "SFDCID" bit, as duplicated IDs on the page may lead to inconsistent behavior. And consider using built-in ways to validate required fields.

几个旁注。请删除id =“SFDCID”位,因为页面上的重复ID可能会导致行为不一致。并考虑使用内置方法来验证必填字段。