[英]add variable on array javascript
我有一個 javascript 代碼,用於從 change 關鍵字更改自動占位符。 這是代碼的最小部分:
$("#addrow").on("click", function () {
counter = $('#myTable tr').length -1;
var newRow = $("<tr>");
var cols = "";
....
cols += '<td><input type="text" name="keyword' + counter + '" placeholder="add keyword in here.." style="width: 425px;"/></td>';
cols += '<td><input type="button" class="ibtnDel" value="-"></td>';
newRow.append(cols);
//if (counter == 4) $('#addrow').attr('disabled', true).prop('value', "You've reached the limit");
$("table.order-list").append(newRow);
counter++;
$("#list_field"+counter).change(function(){
if ($(this).val() == 'all'){
$("input[name=keyword]").attr('placeholder', 'add keyword in here');
}
else if ($(this).val() == 'chrom'){
$("input[name=keyword]").attr('placeholder', 'ex: 8');
} });
});
對我來說,問題是如何在"$("input[name=keyword]").attr('placeholder', 'add keyword in here');"
添加變量“counter "$("input[name=keyword]").attr('placeholder', 'add keyword in here');"
. 是這樣嗎? ->
$("input[name=keyword]").counter.attr('placeholder', 'add keyword in here');
抱歉,我還在學習 javascript。 感謝您的解決方案。
如果你想在占位符的文本中添加一個變量,這里是代碼
var counter =1 ;
$("input[name=keyword]").attr('placeholder', 'add keyword in here '+counter);
如果你想區分每個輸入,你可以添加一個 id
var counter =1 ;
$("input[name=keyword]").attr('id', counter);
你也可以找到一個帶有 css 選擇器 nth-child(number-of-item) 的元素
html
<input class="input" palceholder="blabla1">
<input class="input" palceholder="blabla2">
<input class="input" palceholder="blabla3">
javascript
var counter =1 ;
$(".inputs:nth-child("+counter+")")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.