document.getElementsByClassName返回未定义

时间:2019-01-30 09:57:42

标签: javascript php

  • 当我在下面运行此函数时,得到变量的undefined body。我尝试了其他方法,例如从 document.getElementsByClassName(review).valuedocument.getElementsByClassName(review)[0].value,这给了我
  • 同样尝试过将Dom识别方法更改为id 因此document.getElementByid(review).value仍然为空
// this the java script function  
function review(elem, star, review, vend_id)
{
  document.getElementById(elem).innerHTML = 'Sending review... ';
  console.log(review);
  console.log(elem);
  var review_box = 'review_body'+vend_id;
  console.log(review_box);
  var star = document.getElementsByClassName(star).value;
  var body = document.getElementsByClassName(review).value;
  console.log(body);
  xhttp.onreadystatechange = function(){
        if ( this.readyState == 4 && this.status == 200) {
            document.getElementById(elem).innerHTML = this.responseText;           
        }
      };

      xhttp.open("GET", "event_menu_file/vendor_review.php?star="+star+"&body="+body+"&reviewer_id="+user_id+"&event_id="+event_id+"&vendor_id="+vend_id, true);
      xhttp.send();
}

这是可打印javascript工作的html文档的php

if ($vendor_list_query) 
{   
    while ($vendor_list_result = $vendor_list_query->fetch_assoc()) 
    {
      $vend_id = $vendor_list_result['vendor_id'];

      echo '<div class="reviewBlock">';
      $reviewed_before = $mysqli->query("SELECT id FROM vendor_rating WHERE event_id = '$id' AND vendor_id = '$vend_id' AND rater_id = '$user_id' ")->num_rows;

      echo '<div class="reviewsclass" id="reviewid'.$vend_id.'">';

            if ($reviewed_before < 1) {reviewer($vend_id);}

      review_lister($mysqli, $id, $vend_id, $user_id);
      echo '</div>';// end of the reviewsclass
      echo '</div>';//end of the reviewBlock

    }        
}
else
{
    echo "event evendor list has a minor error".$mysqli->error;
}

function reviewer($vend_id)
{

$star = '<img srcset="star.png" src="star.svg" width="16px" height="16px" onclick="star('.$vend_id.', 1)">
         <img srcset="star.png" src="star.svg" width="16px" height="16px onclick="star('.$vend_id.', 2)">
         <img srcset="star.png" src="star.svg" width="16px" height="16px" onclick="star('.$vend_id.', 3)">
         <img srcset="star.png" src="star.svg" width="16px" height="16px" onclick="star('.$vend_id.', 4)">
         <img srcset="star.png" src="star.svg" width="16px" height="16px" onclick="star('.$vend_id.', 5)">';

  $output = '<div id="v_review_box'.$vend_id.'">';
  $output .= '<div id="rating'.$vend_id.'" style="display: inline-flex;">';
  $output .= $star;

  $output .= '</div>';
  $output .= '<input type="hidden" class="rate_inpt'.$vend_id.'" value="" />';
  $output .= '<div>';
  $output .= '<textarea id="r_body'.$vend_id.'" placeholder="Write a review here.." style="width: 90%; margin-left: 10px; outline: none; border-width: 0 0 0.3px 0;" ></textarea>';
  $output .= '<div style="width: 100%; text-align: right;"><button style="margin:0 10px 10px 0; outline: none;" onclick="review(\'v_review_box'.$vend_id.'\', \'rate_inpt'.$vend_id.'\')">Review</button></div>';
  $output .= '</div>';
  $output .= '</div>';

  echo $output;
}```

3 个答案:

答案 0 :(得分:1)

  1. elem在回调运行时超出范围。您需要复印一份。
  2. 所有这些getElements都是复数的-它们返回一个集合
  3. 我看不到您的“星级”功能,所以我写了一个
  4. 我添加了encodeURIComponent以允许评论文本中包含各种字符

尝试一下-我不再需要其他参数

'<button onclick="review(\''.$vend_id.'\')">Review</button>'

使用

var currentElem, vendor={};
function star(vendid,rating) {
  vendor[vendid].rating = rating;
}
function review(vend_id) {
  currentElem = document.getElementById(elem);
  currentElem .innerHTML = 'Sending review... ';
  var review_box = 'r_body'+vend_id;
  var body = document.getElementById(review_box).value;
  var star = vendor[vendid].rating;
  xhttp.onreadystatechange = function(){
    if ( this.readyState == 4 && this.status == 200) {
        currentElem.innerHTML = this.responseText;           
    }
  };
  xhttp.open("GET", "event_menu_file/vendor_review.php?star="+star+
  "&body="+encodeURIComponent(body)+
  "&reviewer_id="+user_id+"&event_id="+event_id+
  "&vendor_id="+vend_id, true);
  xhttp.send();
}

实际上,您只需要保存vend_id并从那里获取其余的信息。

答案 1 :(得分:0)

通过编写javascript审阅功能和php审阅功能,我使代码可以正常工作。 似乎当“ document.getElementById(elem).innerHTML ='正在发送审阅...';”时首先,它通过使rate_inpt和review_body不可用(将其保持在下面)来更新整个Dom,从而解决了问题。

// the updated javascript function
function review(vend_id)
{
  var elem = 'v_review_box'+vend_id;

  var starinput = document.getElementById('rate_inpt'+vend_id).value;
  var body = document.getElementById('review_body'+vend_id).value;
  body_escaped = encodeURIComponent(body);  
  document.getElementById(elem).innerHTML = 'Sending review... ';
 star="+starinput+"&body="+body_escaped+"&reviewer_id="+user_id+"&event_id="+event_id+"&vendor_id="+vend_id);

 xhttp.onreadystatechange = function(){
    if ( this.readyState == 4 && this.status == 200) {
      document.getElementById(elem).innerHTML = this.responseText;           
    }
  };

  xhttp.open("GET", "event_menu_file/vendor_review.php?star="+starinput+"&body="+body_escaped+"&reviewer_id="+user_id+"&event_id="+event_id+"&vendor_id="+vend_id, true);
      xhttp.send('');
}

对于php函数审阅者,我因此进行了更新

function reviewer($vend_id)
{

$star = '<img class="starImg" srcset="star.png" src="star.svg" width="16px" height="16px" onclick="star('.$vend_id.', 1)">
         <img class="starImg" srcset="star.png" src="star.svg" width="16px" height="16px onclick="star('.$vend_id.', 2)">
         <img class="starImg" srcset="star.png" src="star.svg" width="16px" height="16px" onclick="star('.$vend_id.', 3)">
         <img class="starImg" srcset="star.png" src="star.svg" width="16px" height="16px" onclick="star('.$vend_id.', 4)">
         <img class="starImg" srcset="star.png" src="star.svg" width="16px" height="16px" onclick="star('.$vend_id.', 5)">';

  $output = '<div id="v_review_box'.$vend_id.'">';
  $output .= '<div id="rating'.$vend_id.'" style="display: inline-flex;">';
  $output .= $star;

  $output .= '</div>';
  $output .= '<input type="hidden" id="rate_inpt'.$vend_id.'" value="" />';
  $output .= '<div>';
  $output .= '<textarea id="review_body'.$vend_id.'" placeholder="Write a review here.." style="width: 90%; margin-left: 10px; outline: none; border-width: 0 0 0.3px 0; background-color: #f6f6f6;" ></textarea>';
  $output .= '<div style="width: 100%; text-align: right;"><button style="margin:0 10px 10px 0; outline: none;" onclick="review('.$vend_id.')">Review</button></div>';
  $output .= '</div>';
  $output .= '</div>';

  echo $output;
}

答案 2 :(得分:-2)

您正在调用带有两个参数的函数审阅,并且它定义了4个参数