jQuery(this).attr('id')返回undefined

时间:2015-09-22 05:58:24

标签: javascript jquery html

点击时我试图从锚标记中获取值/ id。代码似乎很好,但我不知道问题在哪里;它每次都返回 undefined nothing

还尝试了jQuery(this).attr("value");jQuery(this).val();jQuery(this).attr("id")

我尝试过的代码如下:

<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

</head>
<body>

    <ul class="switch">
        <li value="1"><a href="#" value="1" id="1">link 1</a></li>
        <li value="2"><a href="#" value="2" id="2">link 2</a></li>
        <li value="3"><a href="#" value="3" id="3">link 3</a></li>
        <li value="4"><a href="#" value="4" id="4">link 4</a></li>
    </ul>

    <div class="clickedId"></div>

    <script>
        jQuery(document).ready(function() {
            jQuery('ul.switch').click('a', function(evt) {
                evt.preventDefault();

                var id = jQuery(this).attr("value");

                jQuery('.clickedId').append(id+'<br>');
            });
        });
    </script>
</body>
</html>

6 个答案:

答案 0 :(得分:2)

您在ul上的绑定事件不在锚点上。因此,事件处理程序中的$(this)引用ul,而ul没有id,它会返回undefined

Demo

&#13;
&#13;
jQuery('ul.switch a').click(function(evt) {
  evt.preventDefault();

  var id = jQuery(this).attr("value");
  jQuery('.clickedId').append(id + '<br>');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<ul class="switch">
  <li value="1"><a href="#" value="1" id="1">link 1</a>

  </li>
  <li value="2"><a href="#" value="2" id="2">link 2</a>

  </li>
  <li value="3"><a href="#" value="3" id="3">link 3</a>

  </li>
  <li value="4"><a href="#" value="4" id="4">link 4</a>

  </li>
</ul>
<div class="clickedId"></div>
&#13;
&#13;
&#13;

您将clickon混淆,您可以使用on,如下所示

jQuery('ul.switch').on('click', 'a', function(evt) {

答案 1 :(得分:2)

您似乎将.click().on()混合在一起。

.click()没有选择器,它将处理程序直接绑定到jQuery对象的元素。

.on()确实采用选择器,但事件名称也是字符串,如下所示:

jQuery('ul.switch').on('click', 'a', function(evt) {

答案 2 :(得分:0)

public class loadMap extends AsyncTask < String, Integer, String > {

     loadMap()
     {

     }

     @Override
     protected String doInBackground(String...params) {
         // TODO Auto-generated method stub

         String uri = "http://maps.google.com/maps/api/geocode/json?address=" + location + "&sensor=false";
         HttpGet httpGet = new HttpGet(uri);
         HttpClient client = new DefaultHttpClient();
         HttpResponse response;
         StringBuilder stringBuilder = new StringBuilder();

         try {
             response = client.execute(httpGet);
             HttpEntity entity = response.getEntity();
             InputStream stream = entity.getContent();
             int b;
             while ((b = stream.read()) != -1) {
                 stringBuilder.append((char) b);
             }
         } catch (ClientProtocolException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }

         JSONObject jsonObject = new JSONObject();
         try {
             jsonObject = new JSONObject(stringBuilder.toString());

             lng = ((JSONArray) jsonObject.get("results"))
                 .getJSONObject(0).getJSONObject("geometry")
                 .getJSONObject("location").getDouble("lng");

             lat = ((JSONArray) jsonObject.get("results"))
                 .getJSONObject(0).getJSONObject("geometry")
                 .getJSONObject("location").getDouble("lat");

             Log.d("latitude", "" + lat);
             Log.d("longitude", "" + lng);
         } catch (JSONException e) {
             e.printStackTrace();
         }
         return null;
     }

     @Override
     protected void onPostExecute(String result) {
         // TODO Auto-generated method stub

         LatLng latLng = new LatLng(lat, lng);

         // Setting the position for the marker
         markerOptions.position(latLng);

         // Setting the title for the marker
         markerOptions.title("");

         // Placing a marker on the touched position
         googleMap.addMarker(markerOptions);

         // Locate the first location

         googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13));
     }
 }

答案 3 :(得分:0)

试试这个

jQuery(document).ready(function() {
    jQuery('ul.switch a').click(function(evt) {
        evt.preventDefault();

        var id = jQuery(this).attr('value');

        jQuery('.clickedId').append(id+'<br>');
    });
});

答案 4 :(得分:0)

You are clicking on UL not anchor tag So try this:

<script>
    jQuery(document).ready(function() {
        jQuery(document).on('click', 'ul.switch a', function(evt) {
            evt.preventDefault();

            var id = jQuery(this).attr("value");

            jQuery('.clickedId').append(id+'<br>');
        });
    });
</script>

答案 5 :(得分:0)

这是正确执行的代码。您的代码中的问题是您尝试在UL标记内的锚点上触发事件

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>

<body>
  <ul class="switch">
    <li value="1"><a href="#" value="1" id="1">link 1</a>
    </li>
    <li value="2"><a href="#" value="1" id="1">link 2</a>
    </li>
    <li value="3"><a href="#" value="1" id="1">link 3</a>
    </li>
    <li value="4"><a href="#" value="1" id="1">link 4</a>
    </li>
  </ul>
  <div class="clickedId"></div>
  <script>
    jQuery(document).ready(function() {
      jQuery('li').click('a', function(evt) {
        jQuery('.clickedId').append($(this).attr("value") + '<br>');
      });
    });
  </script>
</body>

</html>