简体   繁体   English

将复选框中的值放入html输入字段

[英]Place values from checkboxes into html input field

I'm trying to take the values of checkboxes and put them into an input field after they are checked. 我试图将复选框的值选中后将其放入输入字段中。 By using JavaScript, I am able to display these values within a <span> tag. 通过使用JavaScript,我可以在<span>标记中显示这些值。 However, when I try to do this within an <input> tag, they do not appear. 但是,当我尝试在<input>标记内执行此操作时,它们不会出现。

HTML: HTML:

 <table width="680" border="0" cellspacing="0" cellpadding="5">
   <tbody>
     <tr>
       <td width="550" nowrap=""><br>

       <div class="dropdown_container">
         <ul id="select_colors">
            <li>
                <label>
                <input name="categories" type="checkbox" id="categories" value="1" oncheck="cat_id.value=1" />Value 1</label>
            </li>
            <li>
                <label>
                <input name="categories" type="checkbox" id="categories" value="2" oncheck="cat_id.value=2" />Value 2</label>
            </li>
            <li>
                <label>
                <input name="categories" type="checkbox" id="categories" value="3" oncheck="cat_id.value=3" />Value 3</label>
            </li>
            <li>
                <label>
                <input name="categories" type="checkbox" id="categories" value="4" oncheck="cat_id.value=4" />Value 4</label>
            </li>
            <li>
               <label>
               <input name="categories" type="checkbox" id="categories" value="5" oncheck="cat_id.value=5" />Value 5</label>
           </li>

       </ul>
     </div>
     </td>
  </tr>

<!-- Display values from checkboxes within this input tag -->

 <tr>
    <td>       
      <div class="dropdown_box">
          <input name="cat_id" type="text" id="cat_id"  />
      </div>
    </td>
    <td width="550" nowrap="">&nbsp;</td>
  </tr>
 </tbody>
 </table>

JavaScript: JavaScript的:

 $(function () {
    $(".dropdown_container input").change(function () {
       var checked = $(".dropdown_container input:checked");
       var span = $(".dropdown_box input"); 

       span.html(checked.map(function () {
            return $(this).val().replace("_"," ");
        }).get().join(", "));

    });
 });

Use .val() instead of .html() as you are setting value property of the element, not updating innerHTML of the element 在设置元素的value属性时,请使用.val()而不是.html() ,而不是更新元素的innerHTML

 $(function() { $(".dropdown_container input").change(function() { var checked = $(".dropdown_container input:checked"); var span = $(".dropdown_box input"); span.val(checked.map(function() { return $(this).val(); }).get().join(", ")); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <table width="680" border="0" cellspacing="0" cellpadding="5"> <tbody> <tr> <td width="550" nowrap=""> <br> <div class="dropdown_container"> <ul id="select_colors"> <li> <label> <input name="categories" type="checkbox" id="categories" value="1" />Value 1</label> </li> <li> <label> <input name="categories" type="checkbox" id="categories" value="2" />Value 2</label> </li> <li> <label> <input name="categories" type="checkbox" id="categories" value="3" />Value 3</label> </li> <li> <label> <input name="categories" type="checkbox" id="categories" value="4" />Value 4</label> </li> <li> <label> <input name="categories" type="checkbox" id="categories" value="5" />Value 5</label> </li> </ul> </div> </td> </tr> <tr> <td> <div class="dropdown_box"> <input name="cat_id" type="text" id="cat_id" /> </div> </td> <td width="550" nowrap="">&nbsp;</td> </tr> </tbody> </table> 

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

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