检查表单中的复选框,如果是值,则获取属性(Check for checkboxes in form and get attribute instead if value)
我有一个表单作为字符串保存到本地存储。 然而,我的表单有几个复选框,单选按钮等,使用
attribute
而不是value
。 如何执行此类检查并将正确的“值”传递给dataSave函数中的本地存储?function dataSave(){ var mngrFields = {}; $(".mngr-field").each(function(){ if ($(".mngr-field").is("checkbox")){ mngrFields[this.id] = this.attr("checked"); } else { // Default method to get input-text values mngrFields[this.id] = this.value; } }); localStorage.setItem('fieldString', JSON.stringify(mngrFields)); }
获取输入文本值的默认方法在它自己的工作正常,但是当包含复选框检查器时,它会因为错误而中断整个应用程序。
关于如何构建一个我可以用于单选按钮的好检查器的任何想法?
I have a form which is saved to Local Storage as a String. My form however has few checkboxes, radio buttons and so on that use
attribute
instead ofvalue
. How can I perform such a check and pass on right "values" to the Local Storage in my dataSave function?function dataSave(){ var mngrFields = {}; $(".mngr-field").each(function(){ if ($(".mngr-field").is("checkbox")){ mngrFields[this.id] = this.attr("checked"); } else { // Default method to get input-text values mngrFields[this.id] = this.value; } }); localStorage.setItem('fieldString', JSON.stringify(mngrFields)); }
The default method to get input-text values is working just fine on it's own, but when the checkbox checker is included, it breaks the whole application because of an error.
Any ideas on how can I build up a good checker that I might be able to use for radio buttons as well?
原文:https://stackoverflow.com/questions/38451770
最满意答案
根据我的理解,您正在寻找带有功能参数的“注入”类型迭代器。 在php中,注入迭代器是array_reduce,不幸的是它已经坏了,所以你必须编写自己的,例如
function array_inject($ary, $func, $acc) { foreach($ary as $item) $acc = $func($acc, $item); return $acc; }
定义一个回调函数,处理每个项目并返回累加器值:
function boldify($list, $item) { return $list .= "<strong>$item</strong>"; }
其余很容易:
$items = array('foo', 'bar', 'baz'); $res = array_inject($items, 'boldify', ''); print_r($res);
from my understanding, you're looking for an "inject" type iterator with a functional parameter. In php, inject iterator is array_reduce, unfortunately it's broken, so you have to write your own, for example
function array_inject($ary, $func, $acc) { foreach($ary as $item) $acc = $func($acc, $item); return $acc; }
define a callback function that processes each item and returns accumulator value:
function boldify($list, $item) { return $list .= "<strong>$item</strong>"; }
the rest is easy:
$items = array('foo', 'bar', 'baz'); $res = array_inject($items, 'boldify', ''); print_r($res);
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
使用外部数组(Work with external array)[2022-06-09]
如果我正确地理解了你的问题,你应该为你的“外部数组”使用List类型。 它有Add(T item)方法,允许您添加项目的方式与您所描述的完全相同。 假设你的对象是Foo类型的: public void Test() { var externalArray = new List (); var foo1 = MyMethod(externalArray); var foo2 = MyMethod(externalArray); } public Foo MyMethod ... -
用let定义i。 否则,它将尝试访问ids [5]并打印undefined,因为ids数组只有5个元素。 $(function(){ var ids=[1,2,3,4,5]; for(let i=0;i<5;i++){ $("#btn"+ids[i]).click(function(){ alert("btn"+ids[i]); }); } });