简体   繁体   English

使用内部脚本的SVG单选按钮逻辑

[英]SVG Radio Button Logic using Internal Scripting

I am trying to make a radio button control using SVG. 我正在尝试使用SVG进行单选按钮控件。 I wish to allow only one radio button to be selected a time. 我希望一次只能选择一个单选按钮。 I am "selecting" a radio button by setting the size of it's inner-circle element to a predefined size ( 11 ). 我通过将其inner-circle元素的大小设置为预定义的大小( 11 )来“选择”单选按钮。 Below is the code that I thought should be able to do it. 以下是我认为应该可以执行的代码。 I am using classes to keep track of the control's state. 我正在使用类来跟踪控件的状态。 Below is an example svg file. 下面是一个示例svg文件。

<svg
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   version="1.1"
   width="300"
   height="400"
   id="radio">
    <script type="application/ecmascript"><![CDATA[
      var innerCircleExpandedSize = 11;
      function ResponseOptionClicked(evt) {
          console.log('Response option clicked');
          // Remove circle in enabled element
          var enabledElem = document.getElementsByClassName('enabled')[0];
          console.debug(enabledElem);
          if (enabledElem != undefined) {
              console.log('Removing a inner circle');
              enabledElem.getElementsByClassName('response-innercircle')[0].setAttribute('r', 0);
              enabledElem.className.baseVal = enabledElem.className.baseVal.replace('enabled', 'disabled');
              console.log('Removed the inner circle');
          }

          // Add circle in radio button
          console.log('Adding a inner circle');
          evt.currentTarget.getElementsByClassName('response-innercircle')[0].setAttribute('r', innerCircleExpandedSize);
          evt.currentTarget.className.baseVal = evt.currentTarget.className.baseVal.replace('disabled', 'enabled');
          console.log('Added the inner circle');
      }
  ]]></script>    
    <g id="base">
            <g class="response-option disabled" transform="translate(50,150)" onclick="ResponseOptionClicked(evt)" fill="#ffffff">
                <circle class="response-outercircle" r="18" stroke="#000000" stroke-width="3" stroke-miterlimit="4" stroke-opacity="1" stroke-dasharray="none" />
                <circle class="response-innercircle" r="0" fill="#000000" stroke="#000000" />
                <text x="40"  y="6.5" class="response-text" fill="blue" font-size="1.5em">Rarely</text>
            </g>
            <g class="response-option disabled" transform="translate(50,200)" onclick="ResponseOptionClicked(evt)" fill="#ffffff">
                <circle class="response-outercircle" r="18" stroke="#000000" stroke-width="3" stroke-miterlimit="4" stroke-opacity="1" stroke-dasharray="none" />
                <circle class="response-innercircle" r="0" fill="#000000" stroke="#000000" />
                <text x="40"  y="6.5" class="response-text" fill="blue" font-size="1.5em">Sometimes</text>
            </g>
            <g class="response-option disabled" transform="translate(50,250)" onclick="ResponseOptionClicked(evt)" fill="#ffffff">
                <circle class="response-outercircle" r="18" stroke="#000000" stroke-width="3" stroke-miterlimit="4" stroke-opacity="1" stroke-dasharray="none" />
                <circle class="response-innercircle" r="0" fill="#000000" stroke="#000000" />
                <text x="40"  y="6.5" class="response-text" fill="blue" font-size="1.5em">Often</text>
            </g>
            <g class="response-option disabled" transform="translate(50,300)" onclick="ResponseOptionClicked(evt)" fill="#ffffff">
                <circle class="response-outercircle" r="18" stroke="#000000" stroke-width="3" stroke-miterlimit="4" stroke-opacity="1" stroke-dasharray="none" />
                <circle class="response-innercircle" r="0" fill="#000000" stroke="#000000" />
                <text x="40"  y="6.5" class="response-text" fill="blue" font-size="1.5em">Always</text>
            </g>
    </g>
</svg>

When the fist button is pressed the code works as expected and the first button is toggled off, however after more buttons are pressed it keeps trying to toggle off the original button which results in the other buttons having the ability to be enabled. 按下拳头按钮后,代码将按预期工作,并且第一个按钮被关闭,但是在按下更多按钮后,它会继续尝试关闭原始按钮,从而导致其他按钮具有启用功能。 I'm guessing i'm just making a simple logical error however I've spent a while trying to track it down with no success. 我猜我只是在犯一个简单的逻辑错误,但是我花了一段时间试图追踪它,但没有成功。

Have you tried it in firefox? 您是否在Firefox中尝试过? It seems to work for me using that browser.... 使用该浏览器似乎对我有用。

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

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