使用jquery

时间:2018-07-19 01:08:44

标签: jquery

如何使用jquery按用户选择的顺序将选中的复选框放入文本框?每次我选择一个随机复选框时,文本框的值取决于复选框列表的模式。这是我的代码:

<label>Favorite activities and areas: (Please check the box in order of priorities)</label>
<input type="checkbox" class="chckbox" value="C"/>C
<input type="checkbox" class="chckbox" value="C#"/>C#
<input type="checkbox" class="chckbox" value="PHP"/>PHP
<input type="checkbox" class="chckbox" value="Javascript"/>Javascript
<input type="checkbox" class="chckbox" value="Java"/>Java
<hr>
<input type="text" id="subject_areas" placeholder="Selected Checkboxes" disabled>

这是我的jquery代码:

<script type="text/javascript">
  $(document).ready(function(){
   $('.chckbox').click(function(){
   var text = "";
   $('.chckbox:checked').each(function(){
    text += $(this).val() + ', ';
   });
   text = text.substring(0,text.length-2);
   $('#subject_areas').val(text);
   });
 });
</script>

例如,我选择PHP,然后选择C,然后文本框的值为“ C,PHP”,那是错误的。我要的值是“ PHP,C”。请帮我。谢谢。

1 个答案:

答案 0 :(得分:2)

一种方法是设置一个数组变量,您可以根据val toXmlEntityMarshaller: ToEntityMarshaller[department] = ??? val toJsonEntityMarshaller: ToEntityMarshaller[department] = ??? implicit val departmentMarshaller = Marshaller.oneOf(toJsonEntityMarshaller, toXmlEntityMarshaller) def route = parameters("department") { departmentName => // capture the Accept header in case the client did request one optionalHeaderValueByType[Accept] { maybeAcceptHeader => mapRequest ( _ .removeHeader(Accept.name) // set the Accept header based on the department .addHeader(maybeAcceptHeader.getOrElse( Accept(departmentName match { case "hr" ⇒ MediaTypes.`application/xml` case "tech" ⇒ MediaTypes.`application/json` }) )) ) ( // none of our logic code is concerned with the response type complete(department(departmentName)) ) } } 属性来添加或删除该数组变量

checked