【问题标题】:Need a way to get an attr from one of many of the same elements需要一种从许多相同元素之一中获取属性的方法
【发布时间】:2017-09-11 20:36:06
【问题描述】:

我正在尝试访问元素的 id,并切掉 num 以拼凑一个 ajax 调用的 url。我主要使用 JQuery。

我用 .attr() 和 .data() 进行了实验,发现它们只返回第一个匹配项,所以每次都是 1,无论我点击 1、4 还是 54。

.get() 只返回 HTML 标头对象,我还没有找到一种方法来访问它并可能提取一个 id 编号。

代码:

$('.display').click(function(event) {
    event.preventDefault();
    //get category from h4's parent div
    let $parent = $('h4').parent();
    let $cat = $parent.attr('class');
    let cat = String($cat);
    //get id to search for...currently only returning id_1, not unique id
    let $id = $('h4').get();  //need correct method here
    console.log($id);
    //slice off num to use in ajax url req
    if($id.length == 4) {
        id_num = $id.slice(-1) + '/';
    } else if ($id.length == 5) {
        id_num = $id.slice(-2) + '/';
    } else {
        id_num = $id.slice(-3) + '/';
    }
    console.log(id_num);

    $.ajax({
        type: 'GET',
        url: 'https://swapi.co/api/' + $cat + id_num,
        success: function(result) {
            console.log(result);
        }
    })
    <div class="container">
        <h3 id="title">I made the Kessel Run in 12 parsecs.</h3>
            <form id ="searchParams" method="POST">
            Now, I want to know about the
            <input list="cats" name="cats">
            <datalist id="cats">
                <option id="people" value="people"></option>
                <option id="planets" value="planets"></option>
                <option id="films" value="films"></option>
                <option id="species" value="species"></option>
                <option id="vehicles" value="vehicles"></option>
                <option id="starships" value="starships"></option>
            </datalist>
            in Star Wars.
           <input id="getInfo" type="submit">
          </form>
          <div class="display"></div>
        </div>
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
        <script type="text/javascript" src="js/twelveparsecs.js"></script>
    </body>
</html>

谢谢!

【问题讨论】:

  • 你想从哪个元素获取 id?
  • $("input[list='cats']").val() 会这样做吗? (而不是当前的.get()
  • stackoverflow.com/questions/22110737/… 对类似的问题感到抱歉,尽管措辞不佳。

标签: javascript jquery ajax api dom


【解决方案1】:

你的 javascript 不能对 html sn-p 正确,它从第 2 行的失败开始

let $parent = $('h4').parent();  
// never matches on your code, you have no h4 tag
let $cat = $parent.attr('class'); 
// attr is no function of undefined, null pointer exception

获取 id 的方式可以是 this;

<div id="test">

<script>
  console.log($('div').attr('id'));
</script>

如果给定标签超过 1 次,您将返回一个 JQuery 对象数组,该数组在节点上与 $('div') 选择器匹配:

<div id="1">a</div>
<div id="2">b</div>

<script>
  let listOfElements = $('div');
  for(var i = 0; i < listOfElements.length; i++) {
    var singleElementInList = $(listOfElements[i]);
    var id = singleElementInList.attr('id');
    console.log(id);
  }
</script>

get 命令是一个 ajax 调用,用于快速读取 url 的响应

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 2016-03-29
  • 1970-01-01
  • 2011-10-11
相关资源
最近更新 更多