Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.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中使用get方法_Javascript_Php - Fatal编程技术网

如何在javascript中使用get方法

如何在javascript中使用get方法,javascript,php,Javascript,Php,我试图用get方法生成一个表单,但我遇到了一个问题。我不知道如何得到这个值。我做这个代码,但不工作,我不明白为什么 我知道这段代码非常简单,但我想了解如何使用get方法: document.querySelectorbutton.addEventListenerclick,函数{ console.log'Click détecté'; var form=document.createElement'form'; setAttribute'method','GET'; setAttribute'a

我试图用get方法生成一个表单,但我遇到了一个问题。我不知道如何得到这个值。我做这个代码,但不工作,我不明白为什么

我知道这段代码非常简单,但我想了解如何使用get方法: document.querySelectorbutton.addEventListenerclick,函数{ console.log'Click détecté'; var form=document.createElement'form'; setAttribute'method','GET'; setAttribute'action','test1.php'; document.body.appendChildform; 让monSelect=document.createElement'select'; 让monOption=document.createElement'option'; 设置属性“值”,1; monOption.innerText='choice1'; monSelect.appendment; 让monOption2=document.createElement'option'; monOption2.setAttribute'value',2; monOption2.innerText='choice2'; monSelect.Appendment2; form.appendChildmonSelect; 让输入=document.createElement“输入” 输入.setAttribute'type',提交; 输入.setAttribute'value',提交; 表单输入; } ; 奎尔克语 阿琼块
你快到了。对于要提交的表单元素,它需要具有name属性。您只需添加

monSelect.setAttribute('name', 'premier');
然后将查询字符串添加到GET请求中,如下所示:

https://stacksnippets.net/test1.php?premier=1
document.querySelectorbutton.addEventListenerclick,函数{ console.log'Click détecté'; var form=document.createElement'form'; setAttribute'method','GET'; setAttribute'action','test1.php'; document.body.appendChildform; 让monSelect=document.createElement'select'; monSelect.setAttribute'name','premier'; 让monOption=document.createElement'option'; 设置属性“值”,1; monOption.innerText='choice1'; monSelect.appendment; 让monOption2=document.createElement'option'; monOption2.setAttribute'value',2; monOption2.innerText='choice2'; monSelect.Appendment2; form.appendChildmonSelect; 让输入=document.createElement“输入” 输入.setAttribute'type',提交; 输入.setAttribute'value',提交; 表单输入; } ; 奎尔克语 阿琼块