2

我在 IE10 中不接受输入的 js 文件中创建了这个输入框。这是我可以解决的ie问题吗?租金和平方英尺输入是不允许我输入任何数字或文本的。

<div id="POItablediv" class="hideDynamicTableClass add-table" data-property-type="Commercial" style="clear: left;"> 
                    @*<input type="button" onclick="tableToJson('CommercialTable');" value="Get JSON Commercial Data" />*@
                    <p><span class="lb1"></span><input type="button" id="add" value="Add" onclick="insCommercialRow()"/></p>
                    <table id="CommercialTable">
                    <tr>
                    <!--<td>Sno</td>-->
                    <td id="TypeOfSpace">Type Of Space</td>
                    <td id="SquareFeet">Square Footage</td>
                    <td id="Occupancy">Occupancy</td>
                    <td id="TenantName">Tennant Name</td>
                    <td id="RentalRate">Rental Rate</td>
                    <td id="LeaseLength">Lease Length</td>
                    </tr>
                    </table>

                @Html.DropDownList("filterDomain", OpenRoad.Web.Helpers.DropDownLists.GetOptionValues("CommercialSpace", "-1", false), new { @id = "CommercialTypeOfSpace", @style = "display:none;" })
                @Html.DropDownList("filterDomain", OpenRoad.Web.Helpers.DropDownLists.GetOptionValues("NumberOfAmenities", "-1", false), new { @id = "OtherDynamicBeds", @style = "display:none;" })
                @Html.DropDownList("filterDomain", OpenRoad.Web.Helpers.DropDownLists.GetOptionValues("NumberOfAmenities", "-1", false), new { @id = "OtherDynamicBaths", @style = "display:none;" })
                @Html.DropDownList("filterDomain", OpenRoad.Web.Helpers.DropDownLists.GetOptionValues("NumberOfAmenities", "-1", false), new { @id = "OtherDynamicGarage", @style = "display:none;" })
                @Html.DropDownList("filterDomain", OpenRoad.Web.Helpers.DropDownLists.GetOptionValues("CaseOccupant", "-1", false), new { @id = "OtherDynamicOccupied", @style = "display:none;" })
                @Html.DropDownList("filterDomain", OpenRoad.Web.Helpers.DropDownLists.GetOptionValues("CaseOccupant", "-1", false), new { @id = "CommercialOccupancy", @style = "display:none;" })
            @*    @(Html.Telerik().CurrencyTextBox()
                        .Name("CommercialRentalRate")
                        .InputHtmlAttributes(new {id ="CommercialRentalRate" ,style="width:100% display :none!important", placeholder="$"})
                        //.MinValue(0)       

                        .Spinners(false)       
                        .EmptyMessage(string.Empty)
              )*@
                </div>

这是创建输入框的 javascript。

function fillSellerLeadHandlers() {
    //Use the JSON string on the commercial and the other one to render table
    var dataObj = eval(($("#commercialUnitString").val()));   
    var commTable = document.getElementById('CommercialTable');
    var dynaTable = document.getElementById('DynamicTable');

    if (!isEmpty($("#commercialUnitString").val()))
    {
        $.each(dataObj, function (idx, val) {
            var len = commTable.rows.length;
            var new_row = commTable.insertRow(len);
            //Assign different attributes to the element.
            var cell0 = new_row.insertCell(0) //Type Of Space -- DD
            $('select#CommercialTypeOfSpace').clone().attr('style', 'display:block').attr('id', "id" + len + "0").attr('value', val.TypeOfSpace).appendTo(cell0);
            //cell0.appendChild(createElement("text", "id" + len + "0", val.TypeOfSpace));

            var cell1 = new_row.insertCell(1) //Squarefeet
            var element1 = createElement("text", "id" + len + "1", val.SquareFeet);
            element1.setAttribute('class', "numeric");
            cell1.appendChild(element1);

            var cell2 = new_row.insertCell(2)//Occupancy -- DD
            //cell2.appendChild(createElement("text", "id" + len + "2", val.Occupancy));           
            $('select#CommercialOccupancy').clone().attr('style', 'display:block').attr('id', "id" + len + "2").attr('value', val.Occupancy).appendTo(cell2);

            var cell3 = new_row.insertCell(3)//Tenant Name
            cell3.appendChild(createElement("text", "id" + len + "3", val.TenantName));

            var cell4 = new_row.insertCell(4)//Rental Rate
            var element = createElement("text", "id" + len + "4", val.RentalRate);
            element.setAttribute('class', "numeric");
            element.setAttribute('data-val', 'true');
            element.setAttribute('data-val-number', 'Rental Rate needs to be a number');
            element.setAttribute('placeholder', '$');
            //element.attributes['data-val'] = 'true';
            //element.attributes['data-val-number'] = 'Rental Rate needs to be number.';
            cell4.appendChild(element);

            var cell5 = new_row.insertCell(5)//Lease Length
            cell5.appendChild(createElement("text", "id" + len + "5", val.LeaseLength));
            var element = createElement("button", "id", len + "6", "X");
            element.setAttribute("value", "X");
            element.onclick = function () { // Note this is a function
                deleteRow(this, 'CommercialTable');
            };
            var cell6 = new_row.insertCell(6)
            cell6.appendChild(element);

            commTable.appendChild(new_row);
        });
    }
    //Other tables
        var dataObj = eval(($("#otherDynamicSettingsString").val()));      
        if (!isEmpty($("#otherDynamicSettingsString").val()))
          {  $.each(dataObj, function (idx, val) {
                var len = dynaTable.rows.length;
                var new_row = dynaTable.insertRow(len);

                var cell0 = new_row.insertCell(0)
                cell0.appendChild(createElement("text", "id" + len + "0", val.NumOfUnits));

                var cell1 = new_row.insertCell(1) //Beds - DD
                //cell1.appendChild(createElement("text", "id" + len + "1", val.Bedroom));            
                $('select#OtherDynamicBeds').clone().attr('style', 'display:block').attr('id', "id" + len + "1").attr('value', val.Bedroom).appendTo(cell1);

                var cell2 = new_row.insertCell(2)//Baths --DD
                //cell2.appendChild(createElement("text", "id" + len + "2", val.Bathrooms));
                $('select#OtherDynamicBaths').clone().attr('style', 'display:block').attr('id', "id" + len + "2").attr('value', val.Bathrooms).appendTo(cell2);

                var cell3 = new_row.insertCell(3)//Square Footage
                var element1 = createElement("text", "id" + len + "3", val.SquareFeet)
                element1.setAttribute('class', "numeric");
                cell3.appendChild(element1);

                var cell4 = new_row.insertCell(4)//Rental Rate
                var element1 = createElement("text", "id" + len + "4", val.RentalRate);
                element1.setAttribute('class', "numeric");
                element1.setAttribute('placeholder', '$');
                cell4.appendChild(element1);

                var cell5 = new_row.insertCell(5)//Garage - DD
                //cell5.appendChild(createElement("text", "id" + len + "5", val.Garage));
                $('select#OtherDynamicGarage').clone().attr('style', 'display:block').attr('id', "id" + len + "5").attr('value', val.Garage).appendTo(cell5);

                var cell6 = new_row.insertCell(6) //Occupied - DD
                //cell6.appendChild(createElement("text", "id" + len + "6", val.Occupied));
                $('select#OtherDynamicOccupied').clone().attr('style', 'display:block').attr('id', "id" + len + "6").attr('value', val.Occupied).appendTo(cell6);

                var element = createElement("button", "id", len + "7", "X");
                element.setAttribute("value", "X");
                element.onclick = function () { // Note this is a function
                    deleteRow(this, 'DynamicTable');
                };
                var cell7 = new_row.insertCell(7)
                cell7.appendChild(element);

                dynaTable.appendChild(new_row);
            });
        }
        $(".numeric").keypress(function () {
            return (/\d/.test(String.fromCharCode(event.which)))
        });
}
4

1 回答 1

2

第一件事:

createElement 的语法不应该是按照以下几行(注意显式输入标记集),还是您创建了自己的同名方法?

var cell1 = new_row.insertCell(1); //Squarefeet
var element1 = createElement("input");
element1.setAttribute("id", "id" + len + "1");
element1.setAttribute("type", "text");
element1.setAttribute("class", "numeric");
element1.value = val.SquareFeet;
cell1.appendChild(element1);

其次,您的数字事件,我相信是您的问题。这是我不久前写的一个仅限数字的插件:

(function ($) {
    $.fn.onlynumeric = function () {
        $(this).keyup(function (event) {
            if (event.keyCode != 8) {
                $(this).val($(this).val().replace(/\D/g, ""));
            }
        });
    };
} (jQuery));

并将其应用于:

$("input.numeric").onlynumeric();

一些一般性的观点:JavaScript 和 jQuery 的使用,希望你不要介意我越界:

  1. 评估是邪恶的。您可以改用$.parseJSON(string)。你为什么问?好吧,Eval 可以执行某人通过其他方式注入响应/变量中的任何隐藏代码,而 parseJSON 将简单地反序列化数据。否则你可能会像腌鱼一样蜷缩起来。
  2. 您的脚本语法很重,为什么不使用更多的 jQuery 选择器,因为它包含 $(#idofelement) 而不是完整的 document.getElementById("idofelement")?和.prop() & .data()而不是 setAttribute()?
  3. 你知道的,我认为你可以用 jQuery 模板更有效地做到这一点。请参阅此处获取指南。然后,只需预先定义您想要的行标记,然后将相关的 JSON 数据应用到它上面。经典案例,如果你问我...

我希望这不是对你的打击。祝你的应用好运!

于 2013-08-08T22:25:10.987 回答