php如何同时获取select值和option值

php如何同时获取select值和option值,第1张

获取到option的值想实现的效果比如选择的是值一这个option提交后要得到"3"和"值一"这两个数据

[php] view plain copy

[html] view plain copy

<select name="select">

<option value="1">select下拉列表框的值</option>

<option value="2">sdfsd</option>

<option value="3">值一</option>

</select>

echo $_POST['select'];

可以在form中添加一个隐藏域<input type="hidden" id="select_content" name="select_content" />然后在提交的时候,先将所选择的值赋值给隐藏域,然后再将form提交。这里说的赋值是用js *** 作的,当<select name="select" onchange="fuzhi(thisoptions[thisselectedIndex]text)"> function fuzhi(a){documentgetElementById("select_content")value=a;//赋值,咚咚}

documentgetElementById('your select id')options

拿到了指定select的所有option,是个option对象的数组

你遍历就可以 比如第2个option的value:

documentgetElementById('aa')options[1]value

有时候我们需要在一个select中传递两个有关联但不同的值,比如你的这个情况。这时候要利用到option的另一个不常用属性name,用name来存储第二个值,然后在onchange事件中分别用thisoptions[thisselectedindex]name来调用这个值。

全12333

全12321

全123123

在你的程序里,把

之间的内容作为name属性写出来,然后定义一个隐藏的input——optt,再定义好onchange事件,就可以同时获取到value和name两个值了。

纯JS

var e = documentgetElementById("form-field-select-4");

alert(getSelectValues(e));

// Return an array of the selected opion values

// select is an HTML select element

function getSelectValues(select) {

  var result = [];

  var options = select && selectoptions;

  var opt;

  for (var i=0, iLen=optionslength; i<iLen; i++) {

    opt = options[i];

    if (optselected) {

      resultpush(optvalue || opttext);

    }

  }

  return result;

}

JQuery

 var selectedValues = [];    

 $("#form-field-select-4 :selected")each(function(){

     selectedValuespush($(this)val()); 

 });

 alert(selectedValues);

在HTML中,select控件的值等于其当前选中的option的值,所以:

$("select")val(); // 可以获取select当前的值

如果想获取当前select下option的所有的值,则:

var selValue = []; // 定义一个空数组用于接收select下option所有的值

var options = $("select")find("option"); // select下所有的option

for (var i=0; i<optionslength; i++) {

selValuepush(optionseq(i)val()); // 将所有的值赋给数组

}

$('select')val();

这是获得select选中项的值;

如果是要遍历select里面option每一项的值用下面的代码

var select = $('select');

var options = selectfind('option');

for(var i = 0; i < optionslength; i++) {

    alert(options[i]value);

}

以上就是关于php如何同时获取select值和option值全部的内容,包括:php如何同时获取select值和option值、select中option的内容表单提交后jsp中怎么取、如何获取select的option的value值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9489738.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存