【问题标题】:Binding dropdown values using knockout js使用淘汰赛js绑定下拉值
【发布时间】:2013-09-18 15:09:39
【问题描述】:

我有以下JS:

function RowData(Township, Range, Section,Crop, Acres) {
var self = this;

self.Township = Township;
self.Range = Range;
self.Section = Section;
self.Crop = [{ value: "1", crop: "Irrigated Corn" }, { value: "2", crop: "Irrigated Sugar Beets" }, { value: "3", crop: "Irrigated Soybeans" }, { value: "4", crop: "Irrigated Sorghum (Milo, Sudan)" }, { value: "5", crop: "Irrigated Dry Edible Beans" }, { value: "6", crop: "Irrigated Potatoes" }, { value: "7", crop: "Irrigated Alfalfa" }, { value: "8", crop: "Irrigated Small Grains" }, { value: "9", crop: "Range/Pasture/Grass (Brome, Hay, CRP)" }, { value: "10", crop: "Urban Land" }, { value: "11", crop: "Open Water" }, { value: "12", crop: "Riparian Forest and Woodlands" }, { value: "13", crop: "Wetlands" }, { value: "14", crop: "Other Agricultural Lands (Farmsteads, Feedlots, etc.)" }, { value: "15", crop: "Irrigated Sunflower" }, { value: "16", crop: "Summer Fallow" }, { value: "17", crop: "Roads" }, { value: "18", crop: "Dryland Corn" }, { value: "19", crop: "Dryland Soybeans" }, { value: "20", crop: "Dryland Sorghum" }, { value: "21", crop: "Dryland Dry Edible Beans" }, { value: "22", crop: "Dryland Alfalfa" }, { value: "23", crop: "Dryland Small Grains" }, { value: "24", crop: "Dryland Sunflower" }, { value: "25", crop: "Dryland Sugar Beets" }, { value: "26", crop: "Dryland Potatoes" }, { value: "27", crop: "Irrigated Hay" }, { value: "28", crop: "Irrigated Rotation Pasture" }];
self.Acres = Acres;
}
function PBHEPViewModel() {
var self = this;

//Present Conditions
self.present_conditions = ko.observableArray([
    new RowData()
]);
self.present_AddRow = function () {
    self.present_conditions.push(new RowData())
}
self.present_RemoveRow = function (row) { self.present_conditions.remove(row) };

//Future Conditions
self.future_conditions = ko.observableArray([
    new RowData()
]);
self.future_AddRow = function () {
    self.future_conditions.push(new RowData())
}
self.future_RemoveRow = function (row) { self.future_conditions.remove(row) };

//submit the data
self.submit_conditions = function () {

    var PC_data = ko.toJSON(self.present_conditions());
    var FC_data = ko.toJSON(self.future_conditions());

    $.post("/Home/PBHEP", { "PC": PC_data, "FC": FC_data});
}
}
ko.applyBindings(new PBHEPViewModel());

我的 HTML 是:

<tbody data-bind="foreach:future_conditions">

        <tr>
            <td style =" text-align:center">
                <input class="input-small" type="text" placeholder="Township" data-bind="value: Township"></td>
            <td style =" text-align:center">
                <input class="input-small" type="text" placeholder="Range"data-bind="value: Range"></td>
            <td style =" text-align:center">
                <input class="input-small" type="text" placeholder="Section"data-bind="value: Section"></td>
            <td style =" text-align:center">
                 <select name="" data-bind="options: Crop, optionsValue: 'value', optionsText: function (i) { return i.crop }, optionsCaption: 'Choose a Crop...'"></select>
            </td>
            <td style =" text-align:center">
                <input class="input-small" type="text" placeholder="Acres"data-bind="value: Acres"></td>
            <td>
                <button class="btn btn-small btn btn-danger" type="button" data-bind="click: $root.future_RemoveRow"><i class="icon-minus-sign icon-white"></i></button>
            </td>
        </tr>
    </tbody>

当我发布帖子时,我将所有整个数组都放在作物字段中,我只想发布已选择的值。

我该如何实现这一点,我哪里出错了。 提前谢谢你

【问题讨论】:

    标签: javascript jquery asp.net-mvc knockout.js


    【解决方案1】:

    对于单选列表,您使用value 绑定将所选值写入您的视图模型。详情见the documentation顶部附近的注释。

    试试下面的绑定:

    <select name="" data-bind="options: Crop, value: Crop, optionsText: 'crop', optionsCaption: 'Choose a Crop...'"></select>
    

    这会将下拉列表中的文本显示为数组项的crop 属性。它还将下拉列表中的选定值分配给RowData 对象的Crop 属性。

    您可能希望将数组的名称 Crop 更改为 Crops 或其他名称,以将其与 RowData 对象上的单一属性区分开来。确实有点令人困惑。

    【讨论】:

    • 我尝试更改此设置,但我的下拉列表中没有任何内容,是否必须具有 name 属性?
    • 当我使用 value:'value' 它仍然会在帖子中给出整个数组
    • 当您说您的下拉列表中没有任何内容时,我不确定您的意思。你是说下拉菜单是空的吗?或者是一个空白列表?我不确定您为什么要使用该函数来获取 optionsText。我会修改我的答案,看看是否有帮助。
    • 我已经根据您的视图模型的仔细查看更新了上面的绑定。试一试。
    • 我的意思是当我使用 value:value 而不是 optionsvalue:'value' 我没有在下拉列表中填充任何内容,它不会打开
    猜你喜欢
    • 2012-11-29
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 2021-04-28
    • 2014-03-30
    • 1970-01-01
    相关资源
    最近更新 更多