Javascript 获取复选框id并将其附加到文本框中,以逗号分隔
我有以下代码 我只想要我的Javascript 获取复选框id并将其附加到文本框中,以逗号分隔,javascript,jquery,html,Javascript,Jquery,Html,我有以下代码 我只想要我的prescriptioniddiv标签的选中复选框的id 我已经编写了jQuery代码,但没有得到正确的输出。jquery代码如下所示。我想将选中的复选框id附加到隐藏的文本框中 <div class="form-group" id="prescriptionid"> <!-- <label for="cid" class="col-lg-2 col-sm-2 control-label">Patient ID</label&
prescriptionid
div标签的选中复选框的id
我已经编写了jQuery代码,但没有得到正确的输出。jquery代码如下所示。我想将选中的复选框id附加到隐藏的文本框中
<div class="form-group" id="prescriptionid">
<!-- <label for="cid" class="col-lg-2 col-sm-2 control-label">Patient ID</label> -->
<div class="col-lg-10">
<% for (int i=0 ; i < testpre.size(); i++) { if(testpre.get(i).getTest_prescription_id()<11) { %>
<input type="checkbox" name="chk<%=testpre.get(i).getTest_prescription_id()%>" id="<%=testpre.get(i).getTest_prescription_id()%>" value="<%=testpre.get(i).getTest_prescription_name() %>">
<%=testpre.get(i).getTest_prescription_name() %>
<br>
<% } else { %>
<input type="checkbox" name="chk<%=testpre.get(i).getTest_prescription_id() %>" id="<%=testpre.get(i).getTest_prescription_id()%>" value="<%=testpre.get(i).getTest_prescription_name() %>" class="diab">
<%=testpre.get(i).getTest_prescription_name() %>
<br>
<% } %>
<% } %>
</div>
</div>
试试这个:
$(document).ready(function () {
var someObj = {};
someObj.chkArray = [];
$("#prescriptionid input[type=checkbox]").click(function () { // to target the checkbox i added input[type=checkbox]
someObj.chkArray.push(this.id); // this.id is enought, no need for $(this).attr('id')
alert("Handler for .click() called.");
alert("GRANTED: " + someObj.chkArray[0]); // I don't think you need a array, but anyway to show in the alert I added [0]
});
});
一个简单的解决方案是每次从源构造检查数组
$(document).ready(function () {
var someObj = {};
someObj.chkArray = [];
var $checks = $('#prescriptionid input[type="checkbox"]').change(function () {
var ids = $checks.filter(':checked').map(function () {
returnt this.id
}).get();
alert("Handler for .click() called.");
alert("GRANTED: " + ids);
someObj.chkArray = ids;
});
});
我完成了你的要求。检查以下链接页面以显示演示
$(文档).ready(函数(){
})) 谢谢,这是可行的,但是我如何通过逗号segrated将整个数组值附加到文本框中?@rushangpatel你能分享生成的结果吗html@rushangpatel这不是生成的html,您提供了生成html的模板…从哪里可以获得生成的html?@rushangpatel从您的浏览器。。视图源
$(document).ready(function () {
var someObj = {};
someObj.chkArray = [];
var $checks = $('#prescriptionid input[type="checkbox"]').change(function () {
var ids = $checks.filter(':checked').map(function () {
returnt this.id
}).get();
alert("Handler for .click() called.");
alert("GRANTED: " + ids);
someObj.chkArray = ids;
});
});
$( "#prescriptionid" ).click(function() {
var someObj="";
$('input[type="checkbox"]:checked').each(function() {
if(someObj=="")
someObj = this.id;
else
someObj = someObj+","+this.id;
});
//alert(someObj);
$("#sel").val(someObj) ;
});