简体   繁体   English

填充JScript数组以在SELECT上重用

[英]Populating JScript Array for reuse on SELECTs

Forgive me if this is already 'somewhere' on StackOverflow, but I don't 100% know exactly what it would come under... 如果这已经在StackOverflow上的“某处”,请原谅我,但我不100%确切地知道它将发生什么...

I'm trying to retrieve information from a WebService, store this in an array, and then for each <select> within my ASP.Net Datalist, populate it with the array AND have binding attached to an OnChange event. 我试图从Web服务中检索信息,并将其存储在数组中,然后为ASP.Net数据<select>每个<select>用数组填充它,并将绑定附加到OnChange事件。

In other words, I have an array which contains "Yes, No, Maybe" I've an ASP.Net Datalist with ten items, therefore I'd have 10 <Select> s each one having "Yes, No, Maybe" as a selectable item. 换句话说,我有一个包含“是,否,也许”的数组。我有一个包含十个项目的ASP.Net数据列表,因此我要有10个<Select>每个都有“是,否,也许”作为可选项目。 When the user changes one of those <Select> s, an event is fired for me to write back to the database. 当用户更改这些<Select>的一个时,将触发一个事件供我写回数据库。

I know I can use the [ID=^ but don't know how to: 我知道我可以使用[ID = ^,但不知道如何:

a) Get the page to populate the <Select> as it's created with the array b) Assign a Change function per <Select> so I can write back (the writing back I can do easy, it's just binding the event). a)获取页面以填充使用数组创建的<Select> b)每个<Select>分配一个Change函数,以便我可以写回(写回我可以很容易地完成,它只是绑定了事件)。

Any thoughts on this? 有什么想法吗?

I have built a simple example that demonstrates, I think, what you are attempting to accomplish. 我已经构建了一个简单的示例,可以证明您正在尝试实现的目标。 I don't have an ASP.Net server for building examples, so I have instead used Yahoo's YQL to simulate the remote datasource you would be getting from your server. 我没有用于构建示例的ASP.Net服务器,因此我改用Yahoo的YQL来模拟您将从服务器获取的远程数据源。

Example page => http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-multiple-selects-from-datasource.html 示例页面=> http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-multiple-selects-from-datasource.html

Example steps: 示例步骤:

  1. query datasource to get array of select questions 查询数据源以获取选择问题的数组
  2. build HTML of selects 建立选择的HTML
  3. append HTML to page 将HTML附加到页面
  4. attach change event listener to selects 将更改事件侦听器附加到选择
  5. on select value change submit value 选择值更改时提交值

Example jQuery: jQuery示例:

// get list of questions
$.ajax({
  url: url,
  dataType: "jsonp",
  success: function(data) {

    // build string of HTML of selects to append to page
    var selectHtml = "";
    $(data.query.results.p).each(function(index, element) {
      selectHtml += '<select class="auto" name="question'+index+'"><option value="Yes">Yes</option><option value="No">No</option><option value="Maybe">Maybe</option></select> '+element+'<br/>';
    });

    // append HTML to page
    $(document.body).append(selectHtml);

    // bind change event to submit data
    $("select.auto").change(function() {
      var name = $(this).attr("name");
      var val = $(this).val();
      // replace the following with real submit code
      $(document.body).append("<p>Submitting "+name+" with value of "+val+"</p>");
    });
  }
});

Example datasource => http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-multiple-selects-from-datasource-datasource.html 示例数据源=> http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-multiple-selects-from-datasource-datasource.html

Example loaded: 示例加载:

替代文字

Example select value changed: 示例选择值已更改:

替代文字

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

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