【问题标题】:BeautifulSoup: Extract the label of checked radio buttonBeautifulSoup:提取选中单选按钮的标签
【发布时间】:2022-07-22 17:28:05
【问题描述】:

我有单选按钮。如何获取checked 单选按钮的文本?在这个例子中,它是Equal Width

  <li>
      <input class="bRadio" value="1" type="radio" id="main.Option1" name="main.Option" checked="checked">
      <label class="col-form-label">Equal Width</label>
  </li>
  <li>
      <input class="bRadio" value="2" type="radio" id="main.Option2" name="main.Option">
      <label class="col-form-label">Variable Width</label>
  </li>

我可以通过以下方式获得选中的单选按钮。但我不知道如何连接按钮的文本(标签)。

temp = tags.find_all('input', {'class' : 'bRadio', 'type' : 'radio'})
soup = BeautifulSoup(str(temp), 'lxml')
checked = soup.find_all('input', checked="checked")

【问题讨论】:

    标签: python-3.x beautifulsoup radio-button


    【解决方案1】:

    我认为你应该在 JQuery 中这样做:

    <li>
            <input class="bRadio" value="1" type="radio" id="main.Option1" name="main.Option" onchange="getId(this.id)" checked>
            <label for="main.Option1" class="col-form-label">Equal Width</label>
        </li>
        <li>
            <input class="bRadio" value="2" type="radio" id="main.Option2" name="main.Option" onchange="getId(this.id)">
            <label for="main.Option2" class="col-form-label">Variable Width</label>
        </li>
        <p id="demo"></p>
        <script>
            function getId(params){
                let text = $("[for='" + params + "'").text();
                demo.innerText = text;
                // you can use wherever you want
            }
            // Setting a default value
            demo.innerText = "Equal Width";
        </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 2018-04-21
      相关资源
      最近更新 更多