[英]Get checked box values into array using jquery
I have some code which I got from jquery which i modified a bit to that all checked box values will populate a text input element. 我有一些从jquery获得的代码,我对其进行了一些修改,以使所有复选框的值都将填充文本输入元素。
this works perfectly on jsfiddle.. see link to demo http://jsfiddle.net/aAqt2/ 这在jsfiddle上非常有效。.请参阅演示链接http://jsfiddle.net/aAqt2/
but when I try it on my out site, it does not work.. any Idea why? 但是当我在自己的网站上尝试时,它不起作用..知道为什么吗? see my code below
请参阅下面的代码
<html><head>
<script src="/js/jquery.min.js"></script>
<script type="text/javascript">
$('input').on('change', function() {
var values = $('input:checked').map(function() {
return this.value;
}).get();
$('#output').val(values.toString());
});
</script>
</head>
<body>
<from name="test">
<table>
<tr>
<td><input type=checkbox value="1"></td>
</tr>
<tr>
<td><input type=checkbox value="2"></td>
</tr>
<tr>
<td><input type=checkbox value="3"></td>
</tr>
<tr>
<td><input type=checkbox value="4"></td>
</tr>
<tr>
<td><input type=checkbox value="5"></td>
</tr>
<tr>
<td><input type="text" id="output"></td>
</tr>
</table>
</form>
</body>
</html>
You need to wrap your code in a document ready call or place it at the end of the page before the closing body tag. 您需要将代码包装在文档就绪调用中,或者将其放在结束body标记之前的页面末尾。 JSfiddle is doing that for you automatically.
JSfiddle会自动为您执行此操作。
Ex: 例如:
$(document).ready(function () {
$('input').on('change', function () {
var values = $('input:checked').map(function () {
return this.value;
}).get();
$('#output').val(values.toString());
});
});
Wrap your code in a DOM ready function: 将代码包装在DOM ready函数中:
$(document).ready(function() {
//code here
});
You need to put your code inside a ready function. 您需要将代码放入ready函数中。
$(document).ready(function () {
$('input').on('change', function() {
var values = $('input:checked').map(function() {
return this.value;
}).get();
$('#output').val(values.toString());
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.