Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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 如何使用jquery从当前html对象中仅获取一个元素?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何使用jquery从当前html对象中仅获取一个元素?

Javascript 如何使用jquery从当前html对象中仅获取一个元素?,javascript,jquery,html,Javascript,Jquery,Html,我有一个模板 <table class="table" id="mans"> <tr> <td><select class="form-control tag-name"> <option>name</option> </select></td> <td>

我有一个模板

<table class="table" id="mans">

            <tr>

              <td><select class="form-control tag-name">

                  <option>name</option>

              </select></td>
      <td><select class="form-control tag-val">

          <option>name</option>

              </select></td>
              <td id="set-tag-name"> </td>
              <td><button type="button" class="btn btn-info pull-right delete-row">
         -
        </button></td>

            </tr>

          </table>
 <button type="button"  id = "add-row" class="btn btn-info pull-right">
          <i class="glyphicon glyphicon-plus"></i> Add new
        </button>

名称
名称
-
新增
我只想在单击AddNew按钮时添加第一个select元素

这是我的jquery代码,用于添加这两个select

 $("#mydiv").each(function() {
  var tds = '<tr>';
  jQuery.each($('tr:last td', this), function() {
    var x = $(this).html();
     tds += '<td>' + $(this).html() + '</td>';
    tds += '<td>' + x[0]+ '</td>';

  });
  tds += '</tr>';
  if ($('tbody', this).length > 0) {
    $('tbody', this).append(tds);
  } else {
    var getId = el.currentTarget;
    $(getId).append(tds);
  }
});
$(“#mydiv”)。每个(函数(){
var tds=“”;
每个($('tr:last td',this),函数(){
var x=$(this.html();
tds+=''+$(this.html()++'';
tds+=''+x[0]+'';
});
tds+='';
如果($('tbody',this).length>0){
$('tbody',this).append(tds);
}否则{
var getId=el.currentTarget;
$(getId).append(tds);
}
});

如何使用jquery只添加第一个select元素???

U可以使用css选择器在jquery中选择标记,如下所示:

$(".table select:first-child");

名称
名称
-
新增
$(文档).ready(函数(){
$(“#添加行”)。单击(函数(){
$(“#mydiv”)。每个(函数(){
var tds=“”;
每个($('tr:last td',this),函数(){
var x=$(this.html();
tds+=''+$(this.html()++'';
});
tds+='';
如果($('tbody',this).length>0){
$('tbody',this).append(tds);
}否则{
var getId=el.currentTarget;
$(getId).append(tds);
}
});
});
});

您想选择哪个标记?我想选择“您想要整个
选择
元素,包括其
选项
子元素,仅仅是
选择
还是仅仅是选择的
选项
<div id="mydiv">
<table class="table" id="mans">

            <tr>

              <td><select class="form-control tag-name">

                  <option>name</option>

              </select></td>
      <td><select class="form-control tag-val">

          <option>name</option>

              </select></td>
              <td id="set-tag-name"> </td>
              <td><button type="button" class="btn btn-info pull-right delete-row">
         -
        </button></td>
            </tr>
          </table>
 <button type="button"  id = "add-row" class="btn btn-info pull-right">
          <i class="glyphicon glyphicon-plus"></i> Add new
        </button>
</div>   



$(document).ready(function(){
  $("#add-row").click(function(){
$("#mydiv").each(function() {
  var tds = '<tr>';
  jQuery.each($('tr:last td', this), function() {
    var x = $(this).html();
     tds += '<td>' + $(this).html() + '</td>';
  });
  tds += '</tr>';
  if ($('tbody', this).length > 0) {
    $('tbody', this).append(tds);
  } else {
    var getId = el.currentTarget;
    $(getId).append(tds);
  }
});
});
});