简体   繁体   English

通过 Javascript 设置 HTML 元素的属性和样式

[英]Set both attribute and style for HTML elements by Javascript

I have this javascript which works perfectly because I'm using php and looping through lots of data which is best displayed with variables when radio buttons are clicked.我有这个 javascript,它运行良好,因为我正在使用 php 并循环浏览大量数据,当单击单选按钮时,这些数据最好与变量一起显示。 See the code below:请参阅下面的代码:

<script type="text/javascript">

function transferamount() {
 <?php foreach ($wallets as $key => $wallet) : ?>
 if (document.getElementById('<?php echo $key?>').checked) {
 document.getElementById('if<?php echo $key?>').style.display = 'block';
}
else document.getElementById('if<?php echo $key?>').style.display = 'none';
 document.getElementById('if<?php echo $key?>').element.disabled = true;
<?php endforeach; ?>
}
</script>

The issue I am experiencing is being able to set both the attribute and style because I do not want the method = "post" to send all the data for the loop but only those displayed.我遇到的问题是能够设置属性和样式,因为我不希望method = "post"发送循环的所有数据,而只发送显示的数据。 I understand that some browsers will work perfectly even by setting the style to document.getElementById('elementId').style = "display:none;"我知道某些浏览器即使将样式设置为document.getElementById('elementId').style = "display:none;"也能完美运行but then I want to be extra sure that that is the case by setting the attributes of the undesired input elements to disable so that their values will not post.但是我想通过将不需要的输入元素的属性设置为禁用以便它们的值不会发布来更加确定是这种情况。 They all have the same attribute name and that's what I need.它们都具有相同的属性名称,这正是我所需要的。 Thus name = "name" .因此name = "name" There are some restrictions in the input, that's why I cannot just display one input element though they all have the same name attribute .输入中有一些限制,这就是为什么我不能只显示一个输入元素,尽管它们都具有相同的name 属性 How best can I tweak my JavaScript to achieve this?我怎样才能最好地调整我的 JavaScript 来实现这一目标? Thank you in advance.先感谢您。

I rather deleted the loop in the main HTML code and put it as element.innerHTML .我宁愿删除主 HTML 代码中的循环并将其作为element.innerHTML Also I created a div with id="ElementId"我还创建了一个id="ElementId"的 div

<style>
    function transferamount() {
      <?php foreach ($wallets as $key => $wallet) : ?>
      if (document.getElementById('<?php echo $key?>').checked) {
        document.getElementById('ElementId').innerHTML = "<div class='form-group'><label>Amount of <?php echo $wallet?></label><input name='amount' class='form-control' type='number' min='1.00' max='<?php echo ${$key} ?>' placeholder='<?php echo ${$key}?>'></div>";
      }
</style>

With this code, script works perfect.使用此代码,脚本可以完美运行。 Also method = "post" works perfectly. method = "post"很完美。

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

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