Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.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
Javascript 使用jquery捕获元素的名称_Javascript_Jquery_Tags_Element - Fatal编程技术网

Javascript 使用jquery捕获元素的名称

Javascript 使用jquery捕获元素的名称,javascript,jquery,tags,element,Javascript,Jquery,Tags,Element,我有一个名为field\u p\u payment[value]的表单选择元素,我需要捕获该元素并重置选择框,我使用了以下代码 document.getElementsByName("field_p_payment[value]").selectedIndex='0' 但它似乎不起作用,我也尝试了jquery版本作为 $('[name="field_p_payment[value]"]').attr("selectedIndex","0"); 但是$('[name=“field\u p\u

我有一个名为
field\u p\u payment[value]
的表单选择元素,我需要捕获该元素并重置选择框,我使用了以下代码

document.getElementsByName("field_p_payment[value]").selectedIndex='0'
但它似乎不起作用,我也尝试了jquery版本作为

$('[name="field_p_payment[value]"]').attr("selectedIndex","0");
但是
$('[name=“field\u p\u payment[value]”]).prop(“selectedIndex”,“0”)工作


但是我不能使用prop,因为我使用的是最旧版本的jquery。有解决方案吗?

通常
getElementsByName
在HTML集合中返回结果。所以试试看

document.getElementsByName("field_p_payment[value]")[0].selectedIndex='0'

另请阅读以了解更多信息。

@冰淇淋三明治很高兴为您提供帮助!删除我的副本,回答+1