簡體   English   中英

jQuery在輸入文本框中顯示了選中的復選框值,並且取消選中將刪除

[英]jQuery checked checkbox value shown in input textbox and uncheck will remove

我一直在嘗試找到標題中所述的解決方案,其中當您有多個復選框並且當您選中帶有諸如“ blood”之類的值的復選框時,將添加到輸入文本框中。 文本框中的每個選中值都將以空格或逗號分隔。 取消選中復選框時,與之對應的值將從文本框中刪除。

到目前為止,我遇到的最接近的示例就是以下示例: 按單擊順序在文本框中顯示復選框值

我試圖通過使用<input type="text" name="text" id="results" value="" />並具有$li.text(this.value); $li.val(this.value); 但什么也沒出現。 不幸的是,jQuery不是我的專長。

如果可能的話,有人可以說一下嗎?

也許就是您所需要的?

var arr = [];
$('#inputs input').change(function() {
  if (this.checked) {
    arr.push(this.value);
  }
  else {
   arr.splice(arr.indexOf(this.value), 1);
  }
  $('#target').val(arr + '');
});

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>

<body>
  <div id="inputs">
    <input type="checkbox" value="Apple">
    <input type="checkbox" value="Orange">
    <input type="checkbox" value="Pineapple">
    <input type="checkbox" value="Mango">
  </div>
  <input type="text" id="target"/>
</body>
</html>

嘗試這個

小提琴演示

在這里,您只需要在所有復選框上應用click事件。 並在每次點擊時填寫輸入框。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM