【问题标题】:How to get the value of Jpicker (color picker) for each row in a loop如何获取循环中每一行的Jpicker(颜色选择器)的值
【发布时间】:2022-01-05 16:38:25
【问题描述】:

我有两个问题。

  1. 我有一个动态网格。其中用户单击 AddNewRow 按钮并在其中生成一个带有 jpicker 的新行。当然,每一行都有不同的 id。现在,当我在循环中为每一行保存颜色选择器的值时。我正在使用这种语法

    $('#tblCUS tbody tr').each(function () {
    color = '#' + $.jPicker.List[0].color.active.val('ahex');
    Grid+= color + "♥"; 
    

这只给了我第一行的值。对于每个循环,它都给了我第一个选择器的值。如何获取每行颜色选择器的值?我进行了几次谷歌搜索,只有一种语法可用。

  1. 当我的页面重新加载时,颜色选择器会消失。如何使用保存在数据库中的值显示我的颜色选择器?这是我的代码

<DIV id=divsomeid style="WIDTH: 100%">
<TABLE class=display id=tblsometable style="WIDTH: 100%">
<THEAD>
<TR>
<TH class=Greyheader style="WIDTH: 5%">S.No</TH>

<TH class=Greyheader style="WIDTH: 35%">Color</TH>

<TH class=Greyheader>Action</TH></TR></THEAD>
<TBODY>
<TR class=GreyBorder id=tblSBPComments_3 pkid="3">
<TD class=GreyBorder>1</TD>

<TD class=GreyBorder><SPAN class=colorPicker id=clcColor1 value="#00ff00ff"></SPAN></TD>

<TD align=center class=GreyBorder> &nbsp;&nbsp; </TD></TR></TBODY></TABLE><BR></DIV>

页面加载

     $('#tblTable tbody tr .colorPicker').each(function (index) {
          $(this).jPicker({
              window: {
                  expandable: true,
                  position: {
                      x: 'right', // acceptable values "left", 
  "center", "right", "screenCenter", or relative px value
                      y: 'bottom' // acceptable values "top", 
     "bottom", "center", or relative px value
                  },
                  color: {
                      active: $(this).attr('value')
                  }
              }
          });

      });

  });

【问题讨论】:

  • 将每一行值推入一个数组。数组索引将与行索引相同
  • @charlietfl 怎么样?你能帮我吗,因为我进行了几次搜索并浪费了将近一天的时间。

标签: html jquery asp.net color-picker jpicker


【解决方案1】:

每当您的页面加载时,您可以根据 span 标签的 attribute 值为 jPicker 的所有实例添加 active 颜色,以便它显示正确的颜色。即:

$('#tblSBPComments tbody tr .colorPicker').each(function(index) {
    $(this).jPicker({
      window: {
        expandable: true
      },
      color: {
        active: $(this).attr("value") //setting active color on load
      }
    });
  })

然后,要将这些值保存在字符串或数组中,您可以使用被迭代的行的索引并使用它来获取活动颜色,即:

 var color = []; //storing value in array
 var SBPCommentsGrid = "";//or in string
 //loop though table
 $('#tblSBPComments tbody tr').each(function(index) {
      color.push('#' + $.jPicker.List[index].color.active.val('ahex') + "♥"); //push value in array here index will be 0, 1..etc
      SBPCommentsGrid += '#' + $.jPicker.List[index].color.active.val('ahex') + "♥"; 
 })

Working Example

【讨论】:

  • 您好,您的代码在保存时工作正常,但对于获取值,它不起作用@Swati
  • 您是否看到任何错误?您的 html 结构与我在示例中使用的相同?
  • 它没有给我任何错误,但在页面加载中颜色选择器仍然没有显示。我在 question@Swati 中添加了页面加载代码
  • 在颜色之前关闭窗口括号并在color: {.. 中添加active,就像我在我的代码中所做的那样,所以您更新的代码将如下所示:jsfiddle.net/zwdu093p/1
  • 和我的小提琴中的更新代码:jsfiddle.net/L7gq354r
猜你喜欢
  • 2012-09-14
  • 1970-01-01
  • 2013-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-14
  • 2017-12-04
  • 1970-01-01
相关资源
最近更新 更多