简体   繁体   English

使用jQuery / JS,我可以将JS对象的属性直接“绑定”到表单输入元素的值吗?

[英]Using jQuery/JS, can I “tie” together a JS object's property directly to the value of a form input element?

I'm building out an online reservation system for a pet-boarding business in my city and I've hit a wall as to how to efficiently carry out this specific functionality. 我正在为我所在城市的宠物寄宿企业建立在线预订系统,并且在如何有效执行此特定功能方面遇到了障碍。

Quick gist of the user process... 快速了解用户流程...

  1. Some preliminary info is given (client info, pet info, etc etc). 给出了一些初步信息(客户信息,宠物信息等)。
  2. User chooses 2 dates; 用户选择2个日期; the check-in and check-out days. 入住和退房天数。
  3. The program calculates the number of days the client is staying and each day is thrown into a constructor for class "Day". 该程序计算客户端停留的天数,并将每一天扔入类“ Day”的构造函数中。 A ... block is generated for each day and displayed to the user. 每天都会生成一个...块,并显示给用户。 It also might be worth noting that all of the Day objects are stored in an array which is then contained in the mama class "ReservationData"...which besides the Day-array also holds some meta-data pertaining to the reservation itself. 还可能值得注意的是,所有Day对象都存储在一个数组中,该数组然后包含在妈妈类“ ReservationData”中...除了Day数组之外,该类还保存一些与预订本身有关的元数据。

Anyway, my problem is that once the 'dayBlocks' are listed & displayed to the user, the user must then be able to go and check individual "extras" that they want for their pet on that specific day. 无论如何,我的问题是,一旦“ dayBlocks”被列出并显示给用户,用户就必须能够去检查在那个特定日期他们想要的宠物的各个“额外”。

So I've broken it down so that {ReservationData} <--(has an array of)-- {Days} <--(has an array of)-- {Extras}. 因此,我将其分解为{ReservationData} <-(有一个数组)-{Days} <-(有一个数组)-{Extras}。 There are 14 extra services to choose from, so for each day displayed there is a simple list of checkboxes and labels. 有14种其他服务可供选择,因此对于显示的每一天,都有一个简单的复选框和标签列表。

Ideally, I want it setup so that when a checkbox is checked by the user, it directly & immediately alters its corresponding variable within the reservationDataDaysExtraDeepArray accordingly. 理想情况下,我希望对其进行设置,以便当用户选中一个复选框时,它会直接并立即相应地更改ReservationDataDaysExtraDeepArray中的相应变量。 The C-programmer in me wants to tie a pointer to each checkbox, but I'm (at least I think) pretty sure that's not doable with jQuery. 我内部的C程序员想将一个指针绑定到每个复选框,但是我(至少我认为)非常确定这对于jQuery是不可行的。

Although I think I explained it pretty clearly, here's some of the code: 尽管我认为我已经很清楚地解释了它,但是下面是一些代码:

//Day Object/Class
function day(_date, _daynum) {
    this.date = new Date(_date);
    this.dayNum = _daynum;
    this.dayExtras = {
        'YH': false,  //<--- How can I directly manipulate
        'PP': false,  //<--- all of these guys via user-control
        'EE': false,  //<--- of corresponding/assigned 
        'ST': false,  //<--- checkboxes?
        'PT': false,
        'TT15': false,
        'TT30': false,
        'TT45': false,
        'DC': false   //--- Or can I? :/        
    };

    console.log("Day object created with date of " + day.date + " and day-number of " + day.dayNum + ".");

    this.getDayNum = function() { return this.dayNum; }
    this.getDayDate = function() { return this.date; }
}

This is my first question on this site, but I did a lot of searching and am still lost...thanks guys! 这是我在该站点上的第一个问题,但是我进行了很多搜索,但仍然迷路……谢谢大家!

Assuming the name of your checkboxes correspond to the keys in your dayExtras object something like this would do.. 假设复选框的名称与dayExtras对象中的键相对应,则可能会这样做。

//Day Object/Class
function day(_date, _daynum) {
    this.date = new Date(_date);
    this.dayNum = _daynum;
    this.dayExtras = {
        'YH': false,  //<--- How can I directly manipulate
        'PP': false,  //<--- all of these guys via user-control
        'EE': false,  //<--- of corresponding/assigned 
        'ST': false,  //<--- checkboxes?
        'PT': false,
        'TT15': false,
        'TT30': false,
        'TT45': false,
        'DC': false   //--- Or can I? :/        
    };

    console.log("Day object created with date of " + day.date + " and day-number of "+day.dayNum+".");

    this.getDayNum = function() { return this.dayNum; }
    this.getDayDate = function() { return this.date; }
    this.setDayExtras = function (key,val) {
      if(key in this.dayExtras){
        this.dayExtras[key] = val;
      }
    }
}

var myDay = new day(null,null);

$(document).on('change','#myForm input[type="checkbox"]', function () {
  myDay.setDayExtras(this.name,this.checked);
});

Got it working, thanks for pointing me in the right direction guys! 一切正常,感谢您为我指出正确的方向!

function saveExtra(me) {
    var extraName = me.attr('name');
    var extraVal = me.val();
    (reservationData[extraName]).dayExtras[extraVal] = me.prop('checked');
    for (key in reservationData) {
        (reservationData[key]).dump(); //Day data-dump method.
    }
}

this.generateExtrasGrid = function() {
    var fieldName = "day" + this.dayNum + "";
    var exGrid = "";
    exGrid += "<form class='extrasGrid' id='" + this.dayNum + "'>";
    exGrid += "<input type='checkbox' class='extra' name='" + fieldName + "' value='YH' onclick='saveExtra($(this))' />";............
    exGrid += "</form>";
    return exGrid;
}​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM