简体   繁体   English

将PHP变量从HTML传递到Javascript

[英]Pass PHP variables from HTML to Javascript

I want to do something to 'input box' when the radio button is checked, so I'm thinking using JS to solve this, here's part of my HTML code: 我想在选中单选按钮时对“输入框”做一些事情,所以我正在考虑使用JS解决此问题,这是我的HTML代码的一部分:

<input type="radio" class="icheck" id='DELIVER_{{$shoppingcart->id}}' />

$shoppingcart->id is from database, so the ID is dynamic, so now I'm using JS like: $ shoppingcart-> id来自数据库,所以ID是动态的,所以现在我使用JS像这样:

var deliverId = DELIVER_{{ $shoppingcart->id }};
if(document.getElementById(DELIVER_{{$shoppingcart->id}}).checked) {

    //do something

else if (...)
...

but the error is 'shoppingcart is not defined', how can I define it or solve the issue? 但是错误是“未定义购物车”,如何定义它或解决问题?

Thanks in advance!!! 提前致谢!!!

You can get the id on radio box using querySelectorAll document.querySelector('icheck').id and then iterating over all the available radio buttons 您可以使用querySelectorAll document.querySelector('icheck').id id在单选框上获取document.querySelector('icheck').id ,然后遍历所有可用的单选按钮

for(var i=0;i<document.querySelectorAll('.icheck').length;i++){
  var deliverId=document.querySelectorAll('.icheck')[i].id;
  if(document.getElementById(deliverId).checked) {

    //do something

  else if (...)
  ...
}

This will solve your problem. 这样可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM