简体   繁体   English

在数组javascript上添加变量

[英]add variable on array javascript

i have a javascript code for change automatic placeholder from change keyword.我有一个 javascript 代码,用于从 change 关键字更改自动占位符。 This is the smallest part of the code :这是代码的最小部分:

 $("#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');
    } });


});

that was the problem for me is how to add the variable "counter" in "$("input[name=keyword]").attr('placeholder', 'add keyword in here');"对我来说,问题是如何在"$("input[name=keyword]").attr('placeholder', 'add keyword in here');"添加变量“counter "$("input[name=keyword]").attr('placeholder', 'add keyword in here');" . . is like this?是这样吗? -> ->

$("input[name=keyword]").counter.attr('placeholder', 'add keyword in here');

sorry I am still learning about javascript.抱歉,我还在学习 javascript。 thanks for your solution.感谢您的解决方案。

if you want add a variable in text of placeholder here is the code如果你想在占位符的文本中添加一个变量,这里是代码

var counter =1 ; 
$("input[name=keyword]").attr('placeholder', 'add keyword in here '+counter);

if you want discriminate each input you can add an id如果你想区分每个输入,你可以添加一个 id

var counter =1 ; 
$("input[name=keyword]").attr('id', counter);

also you can find an element with css selector nth-child(number-of-item)你也可以找到一个带有 css 选择器 nth-child(number-of-item) 的元素

html html

<input class="input" palceholder="blabla1">
<input class="input" palceholder="blabla2">
<input class="input" palceholder="blabla3">

javascript javascript

var counter =1 ; 
$(".inputs:nth-child("+counter+")")

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

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