单击输入类型文本后检查单选输入

时间:2018-07-31 06:47:15

标签: javascript jquery html

我得到了包含无线电输入和文本输入的简单表格。 我的目标是:单击文本输入时检查广播输入

谢谢

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="md-select">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

  <label for="ul-id"><button type="button" class="ng-binding">Choose your country</button></label>
  <ul role="listbox" id="ul-id" class="md-whiteframe-z1" aria-activedescendant="state2_AK" name="ul-id">
    <input type="text" id="filter" class="searchFilter" placeholder="Type to filter" />
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Afghanistan</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Albania</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Algeria</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Andorra</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Angola</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Antigua & Deps</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Argentina</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Armenia</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Australia</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Austria</li>
    </a>
    <a href="h#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Azerbaijan</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Bahamas</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Bahrain</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Bangladesh</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Barbados</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Belarus</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Belgium</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Belize</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Benin</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Bhutan</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Bolivia</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Bosnia Herzegovina</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Botswana</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Brazil</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Brunei</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Bulgaria</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Burkina</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Burundi</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Cambodia</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Cameroon</li>
    </a>
    <a href="#">
      <li role="option" id="" class="ng-binding ng-scope" tabindex="-1" aria-selected="false">Canada</li>
    </a>
  </ul>

</div>

4 个答案:

答案 0 :(得分:6)

对于args = ['salt', '-G', 'environment:DEV', 'grains.setvals', '{"man_version": "man-dev-2.3"}'] try: p = subprocess.Popen(args, stdin=subprocess.PIPE , stdout=subprocess.PIPE , stderr=subprocess.PIPE ) (stdin,stderr) = p.communicate() except (subprocess.CalledProcessError, OSError ) as err: exit(1) if p.returncode != 0: print("Failure in returncode of command:") id,您应该具有唯一的input,然后您可以通过radio的{​​{1}}关联点击事件,以检查单选输入单击时。

使用jQuery

input
id

使用JavaScript

$('#gr_1_input_4').click(function(){
  $('#gr_1_option_4').prop('checked', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="checkbox-wrapper">
    <label for="gr_1_option_4">
         <input type="radio" id="gr_1_option_4" name="group1" value=""> 
                 Sonstiges: 
          <input type="text" id="gr_1_input_4" name="group1"/>
     </label>
</div>

答案 1 :(得分:2)

您可以为输入文本设置focus事件处理程序,并在其中设置单选按钮的checked属性。

注意:请勿对多个html元素使用相同的ID,因为这样最终会导致javascript / jquery无法用于基于ID的代码。您已将gr_1_option_4用作广播和文本输入的ID。

$(function(){
  $("input[type=text][name=group1]").on("focus", function(){
       $("input[type=radio][name=group1]").prop("checked", true);  
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="checkbox-wrapper">
    <label for="gr_1_option_4">
         <input type="radio" id="gr_1_option_4" name="group1" value=""> 
                 Sonstiges: 
          <input type="text" id="gr_1_option_4" name="group1"/>
     </label>
</div>

答案 2 :(得分:0)

首先,更改元素的ID,元素之间不能有相同的ID。

第二,您可以添加一个事件处理程序,例如

document.getElementById("textId").onclick = function() {myFunction()};

并定义类似的功能

function myFunction() {
   document.getElementById("radioButtonID").checked = true;
}

编辑:我已经回答并看到已经有人对此表示怀疑,但我保留了答案,因为它与其他Jquery不同

答案 3 :(得分:0)

一种替代方法是使用prev

$('input[type="text"]').click(function(){
  $(this).prev('input[type="radio"]').prop('checked', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="checkbox-wrapper">
    <label for="gr_1_option_4">
         <input type="radio" id="gr_1_option_4" name="group1" value=""> 
                 Sonstiges: 
          <input type="text" id="gr_1_input_4" name="group1"/>
     </label>
</div>