Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.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/csharp/267.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 将带有jquery的元素添加到空列表_Javascript_C#_Jquery_Model View Controller - Fatal编程技术网

Javascript 将带有jquery的元素添加到空列表

Javascript 将带有jquery的元素添加到空列表,javascript,c#,jquery,model-view-controller,Javascript,C#,Jquery,Model View Controller,您好,我需要向列表中添加项目,当列表中有元素但列表为空时,它会工作javascript返回未定义的 这是我的函数:当列表为空时,namevalue是未定义的,我认为这是主要问题 function submitAjaxForm(formId) { var form = $("<form></form>"); var targetID = formId.substr(1); form.append($(formId).html()); var nam

您好,我需要向列表中添加项目,当列表中有元素但列表为空时,它会工作javascript返回未定义的

这是我的函数:当列表为空时,namevalue是未定义的,我认为这是主要问题

function submitAjaxForm(formId) 
{
   var form = $("<form></form>");
   var targetID = formId.substr(1);
   form.append($(formId).html());
   var nameValue = $(formId).find('.showDictionariesList').first().find('input[type="hidden"]').first().attr('name');  
   var indexToCut = nameValue.indexOf('[', nameValue.indexOf('[', 0) + 1);
   var featureIndex = nameValue.substring(0, indexToCut);
   var contentVal = $("#dropDownList option:selected").val(); 
   var returnValue = form.serializeArray();
   returnValue[returnValue.length - 1].value = contentVal;
}
Cshtml代码:showDictionariesList是包含我要修改的列表的类

@model MultiShop.Panel.Models.Product.AddProductFeatureTextModel
<div>
        <div id="featureText-@Model.TypeId">

            @Html.HiddenFor(m => m.TypeId)
            @Html.HiddenFor(m => m.IsRequired)
            @Html.HiddenFor(m => m.Name)
            @Html.Label(Model.Name)
            @Html.TextBoxFor(m => m.Value)
            @if (Model.IsRequired)
            { 
                <span class="requiredField"></span>
            }
            @Html.ValidationMessageFor(m => m.Value)
            <div class="showDictionariesList">
                @Html.EditorFor(m => m.ShowDictionaries)
            </div>
            @Html.DropDownListFor(m => m.SelectedDictionaryItemId, new SelectList(Model.DictionaryItems, "Id", "Value", Model.SelectedDictionaryItemId), new { id = "dropDownList" })

            <input type="button" value="Save" onclick="@("submitAjaxForm('#featureText-" + @Model.TypeId + "')")" />
        </div>
        //}
@model MultiShop.Panel.Models.Product.AddProductFeatureTextModel
@Html.HiddenFor(m=>m.TypeId)
@Html.HiddenFor(m=>m.IsRequired)
@Html.HiddenFor(m=>m.Name)
@Html.Label(Model.Name)
@Html.TextBoxFor(m=>m.Value)
@如果(需要型号I)
{ 
}
@Html.ValidationMessageFor(m=>m.Value)
@EditorFor(m=>m.ShowDictionaries)
@Html.DropDownListFor(m=>m.SelectedDictionaryItemId,new SelectList(Model.DictionaryItems,“Id”,“Value”,Model.SelectedDictionaryItemId),new{Id=“dropDownList”})
//}

您的问题是,您试图获取不存在对象的属性 你应该在检查后拿到它

var container = $(formId).find('.showDictionariesList').first();
var nameValueObj = container.find('input[type="hidden"]').first();
if (nameValueObj.length !== 0){
   // input was found so we can get name attribute
   var nameValue= nameValueObj.attr('name');
   var indexToCut = nameValue.indexOf('[', nameValue.indexOf('[', 0) + 1);
   // .........
} else{
   // as far no input elements inside .showDictionariesList your logic to add first one item
   // container.append(
}  

我得到了答案。如果为空,我只需使用基本元素初始化列表。我的编程经验很低,所以我的代码看起来很糟糕,但它可以按照我的要求工作

if (!dictionaryType.HasItems())
        {
            DictionaryDto newForEmptyList = new DictionaryDto();
            List<DictionaryDto> newList = new List<DictionaryDto>();
            newForEmptyList.FeatureId = type;
            newForEmptyList.ProductId = productId;
            newForEmptyList.Value = "";
            newForEmptyList.Id = -1;
            newList.Add(newForEmptyList);
            dictionaryType = newList.ToArray();
        }
if(!dictionaryType.HasItems())
{
DictionaryDto newforeptylist=新DictionaryDto();
List newList=新列表();
newForEmptyList.FeatureId=类型;
newForEmptyList.ProductId=ProductId;
newForEmptyList.Value=“”;
newForEmptyList.Id=-1;
newList.Add(newforeptylist);
dictionaryType=newList.ToArray();
}

您在谈论哪个列表?查看JavaScript错误也会很有帮助。我用cshtml代码编辑了主要帖子。如果你共享编译的
HTML
而不是
cshtml
代码会更好。为什么不在使用
namevalue
之前检查
namevalue
是否不是
未定义的
列表为空<代码>名称值
在列表包含元素时具有正确的值,否则
名称值
未定义的
我知道,但我需要获取此
$(formId)。查找('.showDictionariesList').first().find('input[type=“hidden”]).first()当列表为空时。你知道吗?对不起我;如果列表为空,我会感到困惑。showDictionariesList不包含项,您能否显示您要访问FeatureText[0]的渲染html片段。ShowDictionaries[0]。Id和什么列表为空?我已更新了答案,当您添加第一个元素时,您尝试获取第一个输入和名称属性,但其中还没有项,因此,首先,您应该检查是否有输入,如果没有,您应该使用当前逻辑设置,例如var featureIndex=0;您不应该直接计算索引
var container = $(formId).find('.showDictionariesList').first();
var nameValueObj = container.find('input[type="hidden"]').first();
if (nameValueObj.length !== 0){
   // input was found so we can get name attribute
   var nameValue= nameValueObj.attr('name');
   var indexToCut = nameValue.indexOf('[', nameValue.indexOf('[', 0) + 1);
   // .........
} else{
   // as far no input elements inside .showDictionariesList your logic to add first one item
   // container.append(
}  
if (!dictionaryType.HasItems())
        {
            DictionaryDto newForEmptyList = new DictionaryDto();
            List<DictionaryDto> newList = new List<DictionaryDto>();
            newForEmptyList.FeatureId = type;
            newForEmptyList.ProductId = productId;
            newForEmptyList.Value = "";
            newForEmptyList.Id = -1;
            newList.Add(newForEmptyList);
            dictionaryType = newList.ToArray();
        }