Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.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中将selextbox值发布到php页面_Php_Jquery - Fatal编程技术网

在jquery中将selextbox值发布到php页面

在jquery中将selextbox值发布到php页面,php,jquery,Php,Jquery,我使用jquery按钮单击将selectbox中的所有值发布到PHP页面,但这段代码似乎不起作用。有人能帮忙吗 <script type="text/javascript"> $(function() { $("#btn1").click(function () { var arr= new Array; $("#target_select option").each (function () { // Y

我使用jquery按钮单击将selectbox中的所有值发布到PHP页面,但这段代码似乎不起作用。有人能帮忙吗

<script type="text/javascript">
$(function() {
    $("#btn1").click(function () {
      var arr= new Array; 
       $("#target_select option").each (function () { 
                    // You can push the array on an array 
                    arr.push( $(this).val() ); 
                //alert(  $(this).val()  ); not getting the alert to display too
                    });
      $.post("test.php", { name: arr } );
    });
});

</script>

$(函数(){
$(“#btn1”)。单击(函数(){
var-arr=新数组;
$(“#目标_选择选项”)。每个(函数(){
//您可以将阵列推送到阵列上
arr.push($(this.val());
//警报($(this.val());未显示警报
});
$.post(“test.php”,{name:arr});
});
});
生成尝试使用
.attr(“值”)
而不是
.val()
。我不记得
.val()
是否能在
标记上工作,但我不这么认为

<script type="text/javascript">
$(function() {
    $("#btn1").click(function () {
      var arr= new Array; 
       $("#target_select option").each (function () { 
                    // You can push the array on an array 
                    arr.push( $(this).attr("value") ); 
                //alert(  $(this).attr("value")  ); not getting the alert to display too
                    });
      $.post("test.php", { name: arr } );
    });
});

</script>

$(函数(){
$(“#btn1”)。单击(函数(){
var-arr=新数组;
$(“#目标_选择选项”)。每个(函数(){
//您可以将阵列推送到阵列上
arr.push($(this.attr(“value”));
//警报($(this.attr(“value”);未显示警报
});
$.post(“test.php”,{name:arr});
});
});
-编辑-

我在整理JSFIDLE时发现了与Art(下面的评论)相同的东西

.attr(“value”)
.val()
标记上是相同的

我想删除这个答案,但我认为人们可能会从评论和诸如此类的内容中受益(因为还有另一个答案说明了这一点)。

尝试使用
.attr(“value”)
而不是
.val()
。我不记得
.val()
是否能在
标记上工作,但我不这么认为

<script type="text/javascript">
$(function() {
    $("#btn1").click(function () {
      var arr= new Array; 
       $("#target_select option").each (function () { 
                    // You can push the array on an array 
                    arr.push( $(this).attr("value") ); 
                //alert(  $(this).attr("value")  ); not getting the alert to display too
                    });
      $.post("test.php", { name: arr } );
    });
});

</script>

$(函数(){
$(“#btn1”)。单击(函数(){
var-arr=新数组;
$(“#目标_选择选项”)。每个(函数(){
//您可以将阵列推送到阵列上
arr.push($(this.attr(“value”));
//警报($(this.attr(“value”);未显示警报
});
$.post(“test.php”,{name:arr});
});
});
-编辑-

我在整理JSFIDLE时发现了与Art(下面的评论)相同的东西

.attr(“value”)
.val()
标记上是相同的

我想删除这个答案,但我认为人们可能会从评论和诸如此类的内容中受益(因为还有另一个答案说明了这一点)。

试试看

arr.push( $(this).attr('value') ); 
相反

试试看

arr.push( $(this).attr('value') ); 

相反

您必须确保您要访问的页面位于同一域中。另外,
$.post()
不是提交表单的方式,它是一个Ajax调用,不会在当前页面上加载新页面。这对我来说很有效,将帖子的结果加载到页面的主体中

<html>
<head>
<title>Test Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
    type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {

$("#btn1").bind('click', function () {
    var arr= []; 
    $("#target_select option").each (function () { 
        arr.push( $(this).val() );
    });
    $.post("post.php", { 'name': arr }, function(data){
        $('body').append(data);
    } );
});

});
</script>
</head>
<body>
<form method="POST" action=".">
    <select name="target_select" id="target_select">
        <option value="option1">option1</option>
        <option value="option2">option2</option>
        <option value="option3">option2</option>
    </select>
    <input type="button" value="btn1" id="btn1" />
</form>
</body>
</html>
"; ?> 当我这样做时,我会将以下内容附加到我的页面:


你必须确保你要访问的页面
$.post()
-ing在同一个域上。而且,
$.post()
不是提交表单的方式,它是一个Ajax调用,不会在当前页面上加载新页面。因此,这对我来说很有效,将帖子的结果加载到页面的主体中

<html>
<head>
<title>Test Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
    type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {

$("#btn1").bind('click', function () {
    var arr= []; 
    $("#target_select option").each (function () { 
        arr.push( $(this).val() );
    });
    $.post("post.php", { 'name': arr }, function(data){
        $('body').append(data);
    } );
});

});
</script>
</head>
<body>
<form method="POST" action=".">
    <select name="target_select" id="target_select">
        <option value="option1">option1</option>
        <option value="option2">option2</option>
        <option value="option3">option2</option>
    </select>
    <input type="button" value="btn1" id="btn1" />
</form>
</body>
</html>
"; ?> 当我这样做时,我会将以下内容附加到我的页面:


在我对当前Chrome、Safari和Firefox的测试中,
.attr(“value”)
.val()
直接应用于
时没有区别。在我对当前Chrome、Safari和Firefox的测试中,
.attr(“value”)
.val()之间没有区别
当直接应用于
时。在我对当前Chrome、Safari和Firefox的测试中,
.attr(“value”)
.val()之间没有区别。当直接应用于
时,
.attr(“value”)之间没有区别。在我对当前Chrome、Safari和Firefox的测试中,
.attr(“value”)之间没有区别
.val()
直接应用于
时。