我想使用从变量动态创建的属性选择器,但它只对变量中的字符串的开头做出反应(非动态部分):
public partial class InstallNotify : Window
{
private int totaltime = 30;
DispatcherTimer dispatcherTimer = new DispatcherTimer();
private bool bPostponeBtnClicked = false;
private bool bInstallNowBtnClicked = false;
public bool ISPOSTPONECLICKED
{
get
{
return bPostponeBtnClicked;
}
}
public bool ISINSTALLNOWCLICKED
{
get
{
return bInstallNowBtnClicked;
}
}
/// <summary>
/// Constructor
/// </summary>
public InstallNotify()
{
InitializeComponent();
}
private void InstallNotifyLoaded(object sender, RoutedEventArgs e)
{
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,0,1);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
TimeLeftInSecs.Content = totaltime.ToString();
totaltime--;
if (totaltime < 0)
{
dispatcherTimer.Stop();
this.Close();
}
}
/// <summary>
/// This event handler is called when user clicks on the postpone button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PostponeBtnClicked(object sender, RoutedEventArgs e)
{
bPostponeBtnClicked = true;
this.Close();
}
/// <summary>
/// This event handler is called when user clicks on the Install Now button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InstallNowClicked(object sender, RoutedEventArgs e)
{
bInstallNowBtnClicked = true;
this.Close();
}
}
当我按如下方式使用它时:
var dataKeys = 'td[data-key="name"],td[data-key="code"],td[data-key="date"]';
dataKeys = dataKeys + ',td[data-key="' + fieldsArray[i] + '"]';
当dataKeys:
时,它只对$(dataKeys, this).each(function ()
,"name"
和"code"
键的属性值做出反应
"date"
答案 0 :(得分:-2)
我让你的代码部分在1个片段内工作。我一开始只遇到1个问题:我的浏览器剥离了TD
元素,因为它们不在正确的html表格中,因此选择器找不到td
s
所以我把它放在一张桌子里就可以了。 (见下面的摘录)。
最有可能的错误是代码中的其他位置。也许最好添加一些日志记录或调试,看看你的变量有哪些值。我只给你一个可能的解决方案。
var fieldsArray = ['dynamic', 'serial_no'];
var dataKeys = 'td[data-key="name"],td[data-key="code"],td[data-key="date"]';
dataKeys = dataKeys + ',td[data-key="' + fieldsArray[1] + '"]';
console.log('selector', dataKeys);
$(dataKeys).each(function() {
console.log('i am', this);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td data-key="name">hey</td>
<td data-key="code">hi</td>
<td data-key="excluded">hoho</td>
<td data-key="serial_no">hoho</td>
<tr>
<table>
&#13;