Javascript Jquery更改for循环中的隐藏输入值
如果使用jquery选中复选框,我会尝试更改每个隐藏的输入值 我需要使用for循环,因为html输入字段也是由php循环从db生成的。因此,生成的html代码如下所示: Html:Javascript Jquery更改for循环中的隐藏输入值,javascript,php,jquery,html,Javascript,Php,Jquery,Html,如果使用jquery选中复选框,我会尝试更改每个隐藏的输入值 我需要使用for循环,因为html输入字段也是由php循环从db生成的。因此,生成的html代码如下所示: Html: <input id="gift0" value="1" type="checkbox"> <input id="gift-true0" value="" type="hidden"> <input id="gift1" value="1" type="checkbox"> <
<input id="gift0" value="1" type="checkbox">
<input id="gift-true0" value="" type="hidden">
<input id="gift1" value="1" type="checkbox">
<input id="gift-true1" value="" type="hidden">
<input id="gift2" value="1" type="checkbox">
<input id="gift-true2" value="" type="hidden">
Jquery:
var tot = <?php echo $tot; ?>;
jQuery(function($){
for (var i = 0; i< tot; i++ ){
var chk = '#gift' + i;
var val = '#gift-true' + i;
$(chk).change(function(e) {
for (var a = 0; a<= jml; a++ ){
$(val).val(($(this).is(':checked')) ? "yes" : "no");
}
console.log(e);
});
}
});
var-tot=;
jQuery(函数($){
对于(变量i=0;i
var tot = <?php echo $tot; ?>;
jQuery(function($){
for (var i = 0; i< tot; i++ ){
var chk = $('#gift' + i);
var val = $('#gift-true' + i);
$(chk).change(function(e) {
for (var a = 0; a<= jml; a++ ){
$(val).val(($(this).is(':checked')) ? "yes" : "no");
}
console.log(e);
});
}
});
var-tot=;
jQuery(函数($){
对于(变量i=0;i for(var a=0;a更改代码中的这两个值
var chk = $('#gift' + i).val();
var val = $('#gift-true' + i).val();
更改复选框时只应设置相关输入。您不应在更改事件中为每个输入再次循环
var tot = <?php echo $tot; ?>;
jQuery(function($){
for (var i = 0; i< tot; i++ ){
$('#gift' + i).on('change', function(e) {
// no need for another loop as you should target only relevant input field
$('input#gift-true' + i).val(($(this).is(':checked')) ? "yes" : "no");
});
}
});
var-tot=;
jQuery(函数($){
对于(变量i=0;i
这里不需要循环。只需更改下一个输入的值即可
$('input[type="checkbox"]').change(function(){
var $hidden = $(this).next('input[type="hidden"]');
if ($hidden.length) {
var checked = $(this).is(':checked');
if (checked) {
$hidden.val('yes');
} else {
$hidden.val('no');
}
}
});
//call function for every checkbox which are already on page
$('input[type="checkbox"]').change();
此代码适用于页面上的每个复选框。若要避免此情况,请使用更强的选择器:$('.some class input[type=“checkbox”]”)
我想这就是您想要的
var tot = 3;
jQuery(function($){
for (var i = 0; i< tot; i++ ){
$('#gift' + i).on('change', function(e) {
$(this).next().val(($(this).is(':checked')) ? "yes" : "no");
});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="gift0" value="1" type="checkbox">
<input id="gift-true0" value="no" type="text">
<input id="gift1" value="1" type="checkbox">
<input id="gift-true1" value="no" type="text">
<input id="gift2" value="1" type="checkbox">
<input id="gift-true2" value="no" type="text">
var-tot=3;
jQuery(函数($){
对于(变量i=0;i
将您的id更改为类
<?php
for ($i=0; $i<$total; $i++){
echo "<input type='checkbox' class='gift' name='exm$i' value='1'> <br>
<input type='hidden' name='gift-exm$i' class='gift-true' value=''></td> <br><br>";
}
ps:别忘了给隐藏的输入添加一个名称
$('.gift')。更改(函数(e){
var el=$(本);
el.next('.gift true').val(el.is(':checked')?“是”:“否”);
});
您可以使用next()
函数来执行此操作,因为所有输入都在复选框旁边。请将var tot=3;
替换为var tot=;
并将type=“text”
替换为type=“hidden”
var-tot=3;
jQuery(函数($){
对于(变量i=0;i
与其他一些答案类似,我的答案也消除了for循环
jQuery(function ($) {
// do this if you do not know the state of the generated checkbox
$("input[type='checkbox']").each(function () {
$(this).next().val($(this).prop("checked") ? "yes" : "no");
});
// then this for your event handler
$("input[type='checkbox']").on("change", function (e) {
$(e.target).next().val($(e.target).prop("checked")?"yes":"no");
})
});
您的代码不正确,为什么需要隐藏输入,如何触发此代码?因为我需要获取值,即使我没有选中复选框请参见下面的我的答案,但如果我更改$('(gift-true0').val($(this).is(':checked')?“是”:“否”);它可以工作,但我仍然需要根据复选框更改每个隐藏的输入。他们同意@jafarbtechyeah给出的答案。现在它可以工作了,但我给出了第一个选择答案。顺便说一句,非常感谢@Rasika:)检查你的tot值?
$('.gift').change(function(e) {//remove any loops /keep the change event
var el = $(this);
el.next('.gift-true').val(el.is(':checked') ? "yes" : "no");//select the hidden input using next()
});
jQuery(function ($) {
// do this if you do not know the state of the generated checkbox
$("input[type='checkbox']").each(function () {
$(this).next().val($(this).prop("checked") ? "yes" : "no");
});
// then this for your event handler
$("input[type='checkbox']").on("change", function (e) {
$(e.target).next().val($(e.target).prop("checked")?"yes":"no");
})
});