我有一个令人困惑的问题,但希望你能明白我的意思:
在我的网站上,我试图实现一个选择框,它是根据以前的选择框中的值来更新的。为此,我使用了一个使用值的javascript。例如,复选框中的一个选项如下所示:
<option value="22"> Apple </option>我需要这个值来过滤选择框,但是在我的PHP脚本中,我需要得到那个'Apple'文本。有办法吗?
不好意思问我这个问题,但是网络开发对我来说是新的。
编辑:
这是我用来过滤第二个复选框的java脚本:
$("#select1").change(function() {
if ($(this).data('options') == undefined) {
/*Taking an array of all options-2 and kind of embedding it on the select1*/
$(this).data('options', $('#select2 option').clone());
}
var id = $(this).val();
var options = $(this).data('options').filter('[value=' + id + ']');
$('#select2').html(options);
});如果我试图将筛选器函数中的'value'更改为其他属性,则由于某种原因它无法工作。我一点也不认识JavaScript。
发布于 2013-08-22 10:11:32
尝尝这个
var pName = document.getElementById('selectname');
var name = pName.options[pName.selectedIndex].text;通过隐藏表单字段或ajax请求将name值发送到php脚本,它将包含选项的文本。
发布于 2013-08-22 10:11:41
Apple不是传递给服务器,而是传递给您的值,在本例中是23。您可以看到,当您将公式方法更改为GET时,它将类似于script.php?some_select=23。
解决这一问题的两个解决办法:
第一个(简单的)是:
<option value="Apple" data-filterid="22"> Apple </option>在你的js中:
var options = $(this).data('options').filter('[data-filterid=' + id + ']');因此,您可以在php脚本中获得Apple,而不是22。然后,您可以通过访问data-filterid而不是value来在javascript中过滤它。
第二个解决方案是存储一个关联字典,它将值映射到数字,例如:
<?php
$mapped = array(22 => "Apple", 23=>"Orange");
$value = $mapped[$_GET['option_name']];发布于 2013-08-22 10:17:28
尝尝这个
function getSelectedText(elementId) {
var elt = document.getElementById(elementId);
if (elt.selectedIndex == -1)
return null;
return elt.options[elt.selectedIndex].text;
}
var text = getSelectedText('test');或
this.options[this.selectedIndex].innerHTMLhttps://stackoverflow.com/questions/18377360
复制相似问题