Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JQuery仅从ID为数组的窗体获取数值?_Jquery_Arrays_Forms - Fatal编程技术网

使用JQuery仅从ID为数组的窗体获取数值?

使用JQuery仅从ID为数组的窗体获取数值?,jquery,arrays,forms,Jquery,Arrays,Forms,我在寻找解决方案,但没有找到任何解决方案 我有以下代码: <form name="frm_hs100"> <input id="hs100[1]" name="hs100[1]" type="checkbox"> <input id="hs100[2]" name="hs100[2]" type="checkbox"> </form> $('[name^=hs100]').click(function() { if ($(

我在寻找解决方案,但没有找到任何解决方案

我有以下代码:

<form name="frm_hs100">
    <input id="hs100[1]" name="hs100[1]" type="checkbox">
    <input id="hs100[2]" name="hs100[2]" type="checkbox">
</form>

$('[name^=hs100]').click(function() {
    if ($(event.target).is(":checked")){
        //true
    }
    else {
        //false
      }
    console.log(event.target.id);
    });

$('[name^=hs100]')。单击(函数(){
如果($(event.target).is(“:checked”)){
//真的
}
否则{
//假的
}
日志(event.target.id);
});

console.log打印“hs100[1]”,但如何根据单击的复选框仅获取#1或#2?

使用类和数据属性

$('.my_box')。在('click',function()上{
如果(选中此项){
//真的
}否则{
//假的
}
console.log($(this.data('value'));
});

使用类和数据属性

$('.my_box')。在('click',function()上{
如果(选中此项){
//真的
}否则{
//假的
}
console.log($(this.data('value'));
});

如果ID总是以“hs100[”开头,以“]”结尾,那么您可以简单地去掉这些字母,并用空格替换它们

$('[name^=hs100]').click(function() {
if ($(event.target).is(":checked")){
    //true
}
else {
    //false
  }
var $consolePrint = event.target.id.replace('hs100[','').replace(']','');
console.log($consolePrint);
});
我把打印出来的数字变成了一个变量,以便更容易阅读


我还注意到,无论我是否选中复选框,控制台日志都会输出数字,这就是您希望它执行的操作?

如果ID总是以“hs100[”开头,以“]”结尾,那么您可以简单地去掉这些字母,并用空格替换它们

$('[name^=hs100]').click(function() {
if ($(event.target).is(":checked")){
    //true
}
else {
    //false
  }
var $consolePrint = event.target.id.replace('hs100[','').replace(']','');
console.log($consolePrint);
});
我把打印出来的数字变成了一个变量,以便更容易阅读


我还注意到,无论我是选中还是取消选中复选框,控制台日志都会输出数字,这就是您希望它执行的操作?

使用字符串。替换?使用字符串。替换?很好,看起来数据值是我的最佳选择。非常感谢,看起来数据值是我最好的选择。谢谢,这也是一个很好的解决方案。是的,我需要为每个复选框点击触发谢谢,这也是一个很好的解决方案。是的,每次点击复选框我都需要触发器