Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用js或jQuery将数据id追加到href_Javascript_Jquery_Append_Href_Custom Data Attribute - Fatal编程技术网

Javascript 使用js或jQuery将数据id追加到href

Javascript 使用js或jQuery将数据id追加到href,javascript,jquery,append,href,custom-data-attribute,Javascript,Jquery,Append,Href,Custom Data Attribute,假设我有一张这样的桌子: <table> <tbody> <tr class="card-id" data-id="1"> <td> <a href="http://www.test.nl/">Card 1</a> <input class="option" type="checkbox" /> </td> </tr

假设我有一张这样的桌子:

<table>
  <tbody>
    <tr class="card-id" data-id="1">
      <td>
         <a href="http://www.test.nl/">Card 1</a>
         <input class="option" type="checkbox" />
      </td>
    </tr>
    <tr class="card-id" data-id="2">
      <td>
         <a href="http://www.test.nl/">Card 2</a>
         <input class="option" type="checkbox" />
      </td>
    </tr>
    <tr class="card-id" data-id="3">
      <td>
         <a href="http://www.test.nl/">Card 3</a>
         <input class="option" type="checkbox" />
      </td>
    </tr>
  </tbody>
</table>
还有这个

<a href="http://www.test.nl/card-id=1card-id=2"></a>
但我希望能像这样

<a href="http://www.test.nl/result?card_id=1&card_id=2">Card 1</a>

据我所知,您只是想获取data-id。如果是,您可以通过以下两种方式完成:

第一:

这将为您提供表格的完整TR行

如果你想得到数据id的值,那么你必须使用

var value = test.attr("data-id");

希望这对你有帮助

您可以在复选框的更改事件中执行以下操作

var id = [];//create a empty list of ids
var href = "http://www.test.nl/result?card_id=";//create the start of the url
$('input:checked').each(function(){
   id.push($(this).closest('.card-id').attr('data-id'));//push the id of the selected to the array
})
href = href+id.join('&card_id=');//join the array to a string
console.log(href)//append the formed link to a link tag or in this case to the console
演示:

$document.readyfunction{ $'[type=checkbox].option'.changefunctionev{ var id=[];//创建一个空的id列表 var href=http://www.test.nl/result?card_id=;//创建url的开头 $“输入:已选中”。每个函数{ id.push$this.closest'.card id'.attr'data-id';//将所选对象的id推送到数组中 } href=href+id.join'&card_id=';//将数组连接到字符串 console.loghref; }; }; $document.readyfunction{ $'[type=checkbox].option'.changefunctionev{ var anchor=$this.parent'td'。第一个'a' 变量url=http://www.test.nl/result?; var queryString=; $this.parents'tbody'.find'[type=checkbox]:checked'.eachFunction,cb{ queryString++'card_id='+$cb.parents'tr.card-id'.attr'data-id'+'&'; }; url+=queryString.0,-1;; anchor.attr'href',url; console.logurl; }; };
如何从数据id中进行选择?以便创建另一个标记?代码已编辑,请检查我是否可以做些改进it@RaxWeber不,只需将其附加到现有的HREF中即可
var test=  $("#tbl1");
test.find('[data-id="qty"]')
var value = test.attr("data-id");
var id = [];//create a empty list of ids
var href = "http://www.test.nl/result?card_id=";//create the start of the url
$('input:checked').each(function(){
   id.push($(this).closest('.card-id').attr('data-id'));//push the id of the selected to the array
})
href = href+id.join('&card_id=');//join the array to a string
console.log(href)//append the formed link to a link tag or in this case to the console