如何获取OPTION的TEXT值

时间:2017-07-21 07:41:26

标签: javascript php jquery

在这种情况下,只有主要下拉列表会发生变化,其他下拉列表会发生变化。值会根据它自动更改(因此用户不会更改它们)我尝试使用带有$ _POST的PHP获取选项的TEXT值。但我只能在手动更改其他下拉列表时才能获得此功能。 我试图使用trigger()方法,但它无法获得选项文本值。知道为什么代码无法工作。谢谢。



function setDropDown() {
  var index_name =
    document.getElementsByName('ForceSelection')[0].selectedIndex;
  var others = document.querySelectorAll('.secondary');
  for (var i = 0; i < others.length; i++) {
    others[i].selectedIndex = index_name;
  }
}
<!-- try to get the option text value and pass it to input field-->
<!-- Then in the php code use $_POST[] to retrieve the input value-->
function setTextField(ddl) {
  document.getElementById('make_text').value = ddl.options[ddl.selectedIndex].text;
}

$("select").trigger("change");
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post">
<div><b>Primary dropdown:</b>
  <select name="ForceSelection" id="ForceSelection" onChange="javascript:return setDropDown();">
<option value="" selected>Select</option>
<option value="treatmentid1">treatmentname1</option>
<option value="treatmentid2">treatmentname2</option>
</select>
</div>
<div>
  <b>Other dropdown 1</b>:
  <select class='secondary' id="Qualifications" name="Qualifications" onChange="setTextField(this)">
    <option value="select">select</option>
    <option value="treatmentid1">treatmentname1</option>
    <option value="treatmentid2">treatmentname2</option>
    </select></div>
<input id="make_text" type="hidden" name="make_text" value="" />


<div> <b>Other dropdown 2</b>:
  <select class='secondary' id="Qualifications2" name="Qualifications2">
    <option value="select">select</option>
    <option value="treatmentid1">treatmentname1</option>
    <option value="treatmentid2">treatmentname2</option>
</select>
</form>
&#13;
&#13;
&#13;

PHP代码

   $value =$_POST['make_text'];

2 个答案:

答案 0 :(得分:1)

Html元素<select> onchange不会针对程序化更改触发,您需要自己触发

$(".secondary").trigger("change");

或Id

$("#Qualifications").trigger("change");

问题在于您的隐藏<input>从未拥有该值。如果你删除隐藏在你的代码上,你可以检查它。

因此,当您对值进行POST时,make_text上的值为空字符串。因此,如果在for循环之后触发触发器,那么它将起作用。

function setDropDown() {

var index_name = document.getElementsByName('ForceSelection')[0].selectedIndex;

var others = document.querySelectorAll('.secondary');

for (var i = 0; i < others.length; i++) {

	others[i].selectedIndex = index_name;

}

$("#Qualifications").trigger("change");

}

function setTextField(ddl) {

document.getElementById('make_text').value = ddl.value;

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post">
<div><b>Primary dropdown:</b>
  <select name="ForceSelection" id="ForceSelection" onChange="javascript:return setDropDown();">
<option value="" selected>Select</option>
<option value="treatmentid1">treatmentname1</option>
<option value="treatmentid2">treatmentname2</option>
</select>
</div>
<div>
  <b>Other dropdown 1</b>:
  <select class='secondary' id="Qualifications" name="Qualifications" onChange="setTextField(this)">
    <option value="select">select</option>
    <option value="treatmentid1">treatmentname1</option>
    <option value="treatmentid2">treatmentname2</option>
    </select></div>
<input id="make_text"  name="make_text" value="" />


<div> <b>Other dropdown 2</b>:
  <select class='secondary' id="Qualifications2" name="Qualifications2">
    <option value="select">select</option>
    <option value="treatmentid1">treatmentname1</option>
    <option value="treatmentid2">treatmentname2</option>
</select>
</form>

答案 1 :(得分:0)

我不得不说我认为没有必要使用隐藏的输入文本将数据发送到PHP,因为您只需发布public void loanBook() { boolean loop = true; while (loop) { System.out.println("lol"); -----> loop = false; works fine } System.out.println("lol"); } 的值并在 PHP中检索它喜欢这个<select>

否则,如果您想继续开始,可以将$force = $_POST["ForceSelection"];功能更改为:

setDropDown()