最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing variables to ajax with onclick - Stack Overflow

programmeradmin4浏览0评论

I have a problem with the variables, that I want to pass through ajax to php.

In an php file, I generate some divs with id and name attributes.

livesrc.php

echo "<div class=\"search-results\" id=\"" . $softwareArray['Sw_idn'] . "\" 
name=\"" . $softwareArray['SoftwareName'] . "\" 
onclick=\"addBasket(this.id, this.name)\" >
" . utf8_encode($softwareArray['SoftwareName']) . "</div>";

The relevent part is this:

onclick="addBasket(this.id, this.name)

Sorry for the escapes, in html the div looks like:

<div class="search-results" id="235" 
name="Adobe Acrobat Writer 9.0 Professional"
onclick="addBasket(this.id, this.name)">...</div>

This looks okay, but in the js "addBasket", the variable sw_name is not set. (undefinded)

The JS in the head section:

<script type="text/javascript">
  function addBasket(sw_id, sw_name) 
  {
    alert(sw_name);
    $.post("assets/basket.php",{sw_id: sw_id, sw_name: sw_name}, function(data) 
  { 
      $("#basket").html(data);
    });
  }
</script>

The sw_id is set, but the sw_name is not working. Is the call in html with "this.name" correct?

I have a problem with the variables, that I want to pass through ajax to php.

In an php file, I generate some divs with id and name attributes.

livesrc.php

echo "<div class=\"search-results\" id=\"" . $softwareArray['Sw_idn'] . "\" 
name=\"" . $softwareArray['SoftwareName'] . "\" 
onclick=\"addBasket(this.id, this.name)\" >
" . utf8_encode($softwareArray['SoftwareName']) . "</div>";

The relevent part is this:

onclick="addBasket(this.id, this.name)

Sorry for the escapes, in html the div looks like:

<div class="search-results" id="235" 
name="Adobe Acrobat Writer 9.0 Professional"
onclick="addBasket(this.id, this.name)">...</div>

This looks okay, but in the js "addBasket", the variable sw_name is not set. (undefinded)

The JS in the head section:

<script type="text/javascript">
  function addBasket(sw_id, sw_name) 
  {
    alert(sw_name);
    $.post("assets/basket.php",{sw_id: sw_id, sw_name: sw_name}, function(data) 
  { 
      $("#basket").html(data);
    });
  }
</script>

The sw_id is set, but the sw_name is not working. Is the call in html with "this.name" correct?

Share Improve this question asked Oct 14, 2013 at 15:46 mnlfischermnlfischer 3974 gold badges12 silver badges26 bronze badges 2
  • 1 Try $(this).attr("name") – tymeJV Commented Oct 14, 2013 at 15:49
  • Could you try $(this).attr('name') ? (if you're using jQuery? – Darren Commented Oct 14, 2013 at 15:50
Add a ment  | 

2 Answers 2

Reset to default 5

it's because this.name did not exists if you want to access the attribute you have to call this.getAttribute('name') id are particular and can be access directly.

Personally I will give juste this to the function and extract id and name in the function

 onclick="addBasket(this)";




<script type="text/javascript">
  function addBasket(el) 
  {
    var 
    sw_id = el.id,
    sw_name = $(el).attr('name');

    alert(sw_name);
    $.post("assets/basket.php",{sw_id: sw_id, sw_name: sw_name}, function(data) 
  { 
      $("#basket").html(data);
    });
  }
</script>

You could pass the whole element into your js:

<div class="search-results" id="235" 
name="Adobe Acrobat Writer 9.0 Professional"
onclick="addBasket(this)">...</div>

Then grab what you want in your function

function addBasket(sw) {
    alert( sw.id + ' ' + sw.getAttribute( 'name' ) );
}
发布评论

评论列表(0)

  1. 暂无评论