简体   繁体   English

jQuery / Javascript —根据选择但不显示值显示div

[英]JQuery/Javascript — show div based on select but NOT value

I am using PHP to dynamically populate a select box (a list of equipment). 我正在使用PHP动态填充选择框(设备列表)。 The value is set with the ID of the item selected so I can use it later in my programming. 该值是使用所选项目的ID设置的,因此我以后可以在编程中使用它。

What I need help with... 我需要什么帮助...

Based on the item selected I need to show/hide one of two form fields but I can't use the value as this is the id. 根据选定的项目,我需要显示/隐藏两个表单字段之一,但是我不能使用该值,因为这是id。

What I need to be able to do is read the text in the select box which will contain the item name with either (Service: Set by dates) or (Service: Set by hours) and show either the form field for the date or the form field for the hours? 我需要做的是阅读选择框中的文本,其中包含(服务:按日期设置)或(服务:按小时设置)的项目名称,并显示日期或表单字段表格字段的小时数?

Is this possible? 这可能吗? I can find loads great resources based on the value but not on the text in the select. 我可以根据值而不是选择中的文本找到大量的资源。

I think something like this should work but not sure how to use the text in the select rather than the value. 我认为类似这样的方法应该起作用,但不能确定如何在选择中使用文本而不是值。

$(function() {
$('#system').change(function(){
$('.system').hide();
$('#' + $(this).val()).show();
});
});

Any help would be greatly appreciated!!!! 任何帮助将不胜感激!!!!

Regards 问候

Matt 马特

(NB this is what I'm working with at the mo based on the answers so far, thank you all so much for the help (and for your example Louys Patrice Bessette) - not quite there yet... I was getting an error and managed to track it back to the script not getting the result from the select box. See below now working code! Thanks all!!! (注意,这是到目前为止我正在与mo合作的内容,非常感谢大家的帮助(并以您的示例Louys Patrice Bessette为例)-尚不存在...我遇到了错误并设法将其追溯到脚本,但未从选择框中获得结果。请参见下面的工作代码!

<div class="col">
<div class="form-group">
<label for="system_select">Equipment or System to be serviced</label>
<select class="form-control" name="system_select" id="system_select">
<option></option>
<?php
$systems = DB::getInstance()->get('ym_system', 'vessel_id', '=', $vid);
foreach ($systems->results() as $system) {
?><option data-type="<?php echo $system->service_type ?>" value="<?php echo $system->system_id ?>"><?php echo $system->system_name; ?></option> <?php
}
?>
</select>
</div>
</div>
</div>
<script type="text/javascript">
$( document ).ready(function() {
$("#due_dates").hide();   // Hide both divs
$("#due_hours").hide();   // Hide both divs
$('#system_select').change(function(){
var dataType = $(this).find(':selected').attr('data-type')  // should be "Set by dates" or "Set by hours"
if (dataType == 'Set by dates') {
$("#due_hours").hide();
$("#due_dates").show();
} else if (dataType == 'Set by hours') {
$("#due_dates").hide();
$("#due_hours").show();
}
});
});
</script>
<div class="row">
<div class="col-md-3" id="due_date">
<div class="form-group">
<label for="service_date">Next Service (Set by dates)</label>
<input type="date" class="form-control" name="service_date" id="service_date" value="<?php echo escape(Input::get('service_date')); ?>" autocomplete="off">
</div>
</div>
<div class="col-md-3" id="due_hour">
<div class="form-group">
<label for="service_hours">Next Service (Set by hours)</label>
<input type="number" class="form-control" name="service_hours" id="service_hours" value="<?php echo escape(Input::get('service_hours')); ?>" autocomplete="off">
</div>
</div>
</div>

I think that you PHP $system->service_type; 我认为您的PHP $system->service_type; is echoing either "Set by dates" or "Set by hours" . 回显"Set by dates""Set by hours"

If you echo that in a data attribute like this: 如果您在这样的数据属性中回显该信息,则:

<select class="form-control" name="system_select" id="system_select">
  <option></option>
  <?php
  $systems = DB::getInstance()->get('ym_system', 'vessel_id', '=', $vid);
    foreach ($systems->results() as $system) {
      ?><option data-type="<?php echo $system->service_type; ?>" value="<?php echo $system->system_id ?>"><?php echo $system->system_name . ' (Service: '. $system->service_type .')'; ?></option> <?php
    }
  ?>
</select>

Then, in jQuery, you could use it like this to decide to show <div id="due_hours" class="col-md-3"> or <div id="due_hours" class="col-md-3"> . 然后,在jQuery中,您可以像这样使用它来决定显示<div id="due_hours" class="col-md-3"><div id="due_hours" class="col-md-3">

$(function() {
  $('#system').change(function(){
    $('.system').hide();  // I don't know what this is for...

    $("div[id=^'due']").hide();   // Hide both divs

    var dataType = $(this).data("type");  // should be "Set by dates" or "Set by hours"
    var type = dataType.split(" by ")[1];  // should be "dates" ot "hours"

    $("#due_"+type).show();
  });
});

Now be carefull with the s on dates ... 现在请注意dates上的s ...
Try this out... Console.log the values to make sure you have the correct one to match the id to show. 试试看... Console.log值,以确保您具有与显示的ID相匹配的正确值。

;) ;)

If $(".classname").text() is not working you could try to add that info that you need in a "data" attribute. 如果$(".classname").text()无法正常工作,则可以尝试在“数据”属性中添加所需的信息。

Something like this -> 像这样->

<option data-service="service name" class="myClass">15</option>

Using data attributes can give you a lot of freedom on what you can add. 使用数据属性可以给您很大的自由,可以添加什么。 You could add lots of information that a normal user cant read (without inspecting element). 您可以添加许多普通用户无法阅读的信息(无检查元素)。

Then you would simply do something like this -> 然后,您只需执行以下操作->

$(".myClass").on("click", function() {
    var serviceData = $(this).attr("data-service");
})

You can then do if checks, compare and whatever you need. 然后,您可以进行检查,比较以及所需的一切。

EDIT: Or you can use your approach with on.change and get the selected data attr, it would probably be better 编辑:或者您可以使用on.change的方法,并获取所选的数据attr,这可能会更好

If you change your select into something like this: 如果您将选择更改为如下所示:

<select class="form-control" name="system_select" id="system_select">                
   <option data-hide='#due_date' data-show='#due_hours'>Show Hours</option>
   <option data-hide='#due_hours' data-show='#due_date' value="B">Show Dates</option>
</select>

And your jquery like this: 和你的jQuery这样的:

$('#system_select').change(function () {
    $($(this).children("option:selected").attr("data-hide")).hide();
    $($(this).children("option:selected").attr("data-show")).show();
});

It could work. 它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM